TIP: Complete list of methods can be found from Customizing Flex Objects section.
count(): int
Count the number of directories registered to Flex.
Returns:
int
Number of Directories{% set flex = grav.get('flex') %}
Flex has {{ flex.count() }} enabled directories.
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var int $count */
$count = $flex->count();
getDirectories( [names] ): array
Get list of directories.
Parameters:
array
)Returns:
array
list of DirectoriesTIP: If no list of names was provided, method returns all directories registered to Flex.
{% set flex = grav.get('flex') %}
{# Get all directories #}
{% set directories = flex.directories() %}
{# Get listed directories #}
{% set listed_directories = flex.directories(['contacts', 'phonebook']) %}
{# Do something with the directories #}
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexInterface;
use Grav\Framework\Flex\Interfaces\FlexDirectoryInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var FlexDirectoryInterface[] $directories */
$directories = $flex->getDirectories();
// = ['contacts' => FlexDirectory, ...]
/** @var FlexDirectoryInterface[] $directories */
$listedDirectories = $flex->getDirectories(['contacts', 'phonebook']);
// = ['contacts' => FlexDirectory]
/** @var array<FlexDirectoryInterface|null> $directories */
$listedDirectoriesWithMissing = $flex->getDirectories(['contacts', 'phonebook'], true);
// = ['contacts' => FlexDirectory, 'phonebook' => null]
TIP: You may want to make sure you return only the directories you want to.
hasDirectory( name ): bool
: Check if directory exists.
Parameters:
string
)Returns:
bool
True if found, false otherwise{% set flex = grav.get('flex') %}
Flex has {{ not flex.hasDirectory('contacts') ? 'not' }} contacts directory.
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var bool $exists */
$exists = $flex->hasDirectory('contacts');
getDirectory( name ): Directory | null
Get a directory, returns null if it was not found.
Parameters:
string
)Returns:
object
)null
Directory not found{% set flex = grav.get('flex') %}
{# Get contacts directory (null if not found) #}
{% set directory = flex.directory('contacts') %}
{# Do something with the contacts directory #}
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexInterface;
use Grav\Framework\Flex\Interfaces\FlexDirectoryInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var FlexDirectoryInterface|null $directory */
$directory = $flex->getDirectory('contacts');
if ($directory) {
// Directory exists, do something with it...
}
Check what you can do with Flex Directory
getObject( id, directory ): Object | null
Get an object, returns null if it was not found.
Parameters:
string
)string
)Returns:
object
)null
Object not found{% set flex = grav.get('flex') %}
{% set contact = flex.object('ki2ts4cbivggmtlj', 'contacts') %}
{# Do something #}
{% if contact %}
{# Got Bruce Day #}
{{ contact.first_name|e }} {{ contact.last_name|e }} has a website: {{ contact.website|e }}
{% else %}
Oops, contact has been removed!
{% endif %}
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexInterface;
use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var FlexObjectInterface|null $object */
$object = $flex->getObject('ki2ts4cbivggmtlj', 'contacts');
if ($object) {
// Object exists, do something with it...
}
Check what you can do with Flex Object
getCollection( directory ): Collection | null
Get collection, returns null if it was not found.
Parameters:
directory
Name of the directory (string
)Returns:
object
)null
Directory not found{% set flex = grav.get('flex') %}
{% set contacts = flex.collection('contacts') %}
{# Do something #}
<h2>Ten random contacts:</h2>
<ul>
{% for contact in contacts.filterBy({published: true}).shuffle().limit(0, 10) %}
<li>{{ contact.first_name|e }} {{ contact.last_name|e }}</li>
{% endfor %}
</ul>
use Grav\Common\Grav;
use Grav\Framework\Flex\Interfaces\FlexCollectionInterface;
use Grav\Framework\Flex\Interfaces\FlexInterface;
/** @var FlexInterface $flex */
$flex = Grav::instance()->get('flex');
/** @var FlexCollectionInterface|null $collection */
$collection = $flex->getCollection('contacts');
if ($collection) {
// Collection exists, do something with it...
}
Check what you can do with Flex Collection
Found errors? Think you can improve this documentation? Simply click the Edit link at the top of the page, and then the icon on Github to make your changes.
Powered by Grav + with by Trilby Media.