I want to include a content page inside one of my CMS pages, like follows:
<div class="container">
{% content "someContent" %}
</div>
However, someContent might not exist. I want to check if my content page exists before including it, or find some way to have a default return value if it doesn't exist, but I can't seem to find a way to do so.
I tried extending Twig with a function which checks the existence of the content, but could not get the function to register on the page (which according to #2184 is not possible due to plugins and the CMS having different Twig environments).
Anyway, is there a quick (or _a_ way) to achieve what I'm looking for?
Sorry for the late reply, try this:
{% if content('someContent') %}
{% content "someContent" %}
{% endif %}
The GitHub issue list is for reporting bugs in the OctoberCMS code base specifically. Please try the following support avenues for getting support with using October:
I tried that before opening this issue, but it sadly does not seem to work. It was also less a question and more a suggestion, since I am under the impression that it is not possible at the moment.
@niekvanderkooy Did
{% if content('someContent') %}
{% content "someContent" %}
{% endif %}
work for you?
Nope. Sorry, that was the that I was referring to in my previous response ;)
@niekvanderkooy The context difference between the two contexts is one way, plugin extensions will be available in the CMS Twig context but the CMS Twig extensions aren't available in the system twig environment. What wasn't working about your function that you registered?
If I extend twig like documented here, using the function in a page gives me the Unknown "functionName" function. error
Do you have a code example of what you're doing?
@niekvanderkooy did you manage to solve the problem, how to check if content exist?
In the end I worked around it because in any way I tried it I couldn't get it working. But would be a nice feature to have OOTB.
@zlobec @LukeTowers The only solution available today is to extend twig in your own plugin (as Niek originally pointed out):
http://octobercms.com/docs/plugin/registration#extending-twig
_Custom Twig filters and functions can be registered in the CMS with the registerMarkupTags method of the plugin registration class._
plugins/<author>/<plugin>/Plugin.php /**
* @return array
*/
public function registerMarkupTags()
{
return [
'functions' => [
'content_exists' => [$this, 'contentExists']
]
];
}
/**
* Checks if a content file exists in the active theme
* @param string $file File name, including the extension (.htm, .md, .txt)
* @return bool
*/
public function contentExists($file = null)
{
return file_exists(themes_path(Theme::getActiveThemeCode() . '/content/' . $file));
}
Then in your layout or partial, use the new function to determine it exists before rendering it:
themes/<theme>/layouts/default.htm<div class="container">
{% if content_exists('someContent.htm') %}
{% content 'someContent.htm' %}
{% endif %}
</div>
Hope this helps others who stumble across this unfortunate problem!
@HenrikR didn't work for multilingual content files content.es.htm ...
I added this instead ( borrowed the translate plugin )
public function contentExists($file = null)
{
return EventRegistry::instance()
->findTranslatedContentFile( new Controller(), $file);
}
Why not to add this method into core like "placeholder()", "partial()" methods?
@GinoPane it sounds like a good doc addition more to me, then adding it to the core.
@w20k but wouldn't it be more consistent to have similar methods for all content types? I see no reason why "content" method was missed.
Most helpful comment
@zlobec @LukeTowers The only solution available today is to extend twig in your own plugin (as Niek originally pointed out):
plugins/<author>/<plugin>/Plugin.phpThen in your layout or partial, use the new function to determine it exists before rendering it:
themes/<theme>/layouts/default.htmHope this helps others who stumble across this unfortunate problem!