How to apply if statement for page [duplicate] - twig

how to use or condition for checking the page in october cms ?
I have attached a screenshot of my code above.

you can use it like that
{% if this.page.baseFileName == 'home' or this.page.title == 'Page not found (404)' %}
okay I updated the answer. please check now

Related

How to run an object's method in twig without rendering what it returns [duplicate]

I have seen the twig documentation about do tag, but I don't understand its use/useful.
The docs says the follow:
The do tag works exactly like the regular variable expression ({{ ...
}}) just that it doesn't print anything:
and show an example:
{% do 1 + 2 %}
What to solve exactly this tag ?
Good question! I found a link on GitHub to when this was proposed which might add some further info:
Sometimes you want to do things, or call some stuff, and ignore the output. For example if you use a |shift filter to remove some data from an array, doing {{ arr|shift }} will output the removed item, which is not always desirable.
Of course it's possible to do {% set null = arr|shift %}, which won't
output anything, but it also looks weird.
The example in the docs is poor as it explains nothing at all, as you pointed out.

Pagination works for archive of CPT but throws 404 error for single-cpt.php

I can use Timber pagination in archive-law.php for the archive page (/law/page/2) of my custom post type, "law", but with the same codes in single-law.php, the second page (/law/cpt-post-slug/page/2) shows the 404 error. How can I fix that?
That’s how it’s supposed to work. Pagination only works for archive templates like archive.php or home.php, where you have a query with multiple results that can be paginated. It will not work for singular templates like single.php.
However, you could use next_post_link() and previous_post_link(), which display a link to the next or previous post which exists in chronological order from the current post.
In Timber, these functions are available as post.next and post.prev. Here’s an example for how you could use it in in a singular template in Twig:
{% if post.next %}
<h3>Next Article</h3>
{{ post.next.title }}
{% endif %}

Grav CMS: how to show/hide parts of the page depending on conditions?

The Grav's documentation clearly describes how a whole page or a folder could be hidden from unregistered users. It also describes how a whole page could be seen only by particular user groups.
But what about pieces of a page, let's say, some links or a private info I want to show on some conditions?
Ok, for registered users I found a snippet at Login plugin docs:
{% if grav.user.authenticated %}
content for registered users goes here
{% endif %}
But going wider - how can I show/hide pieces of a particular page depending on some custom logic in PHP code, i.e. not necessarily user related?
I'm thinking about a twig/shortcode plugin, something like:
{% if some.custom.condition.or.PHP.function %}
hidden content goes here
{% endif %}
or
[hidden_if_something] hidden content goes here [/hidden_if_something]
But not sure how exactly this should be implemented. So working examples would be appreciated. Thanks.
There is a recipe in the Grav documentation here. This provides an example of how to render the output of a PHP code result in a twig template.
In the example they create a plugin, and implement a twig extension providing access to a php function. They can then simply call that php function like in a twig template.
{{ example() }}
Following that example, you can implement whatever logic you would like in php, and call the function in a twig if statement.
{% if example() == true %}
your conditional output
{% endif %

Yii 2 registerJs in twig templates

In the Yii 2.0 guide it says you can register Javascript code in a twig template like this:
{registerJs key='show' position='POS_LOAD'}
$("span.show").replaceWith('<div class="show">');
{/registerJs}
Tried this but it will simply output the whole snippet {registerJs ... as text on the page without adding it to the page's Javascript Code.
Similar commands like registerJsFile or registerCss aren't working either.
Am I missing something? Thanks!
EDIT:
As Mihai P. noted below, {registerJs} is syntax for Smarty templates. So, the question is: is there a similar way to register inline JS in Twig templates? The documentation only mentions registering assets.
You can use this in your Twig template, which is the current view, to call registerJs.
{% set script %}
var app = new Vue({
el: '#vue-widget',
data: {
message: 'Hello Vue!'
}
})
{% endset %}
{{ this.registerJs(script,4,'vue-widget') }}
The question is 3 years old, I put it here for someone runs into a similar problem.
You can use this in the Twig template with View constant like below:
{{ this.registerJs('$("span.show").replaceWith(\'<div class="show">\');', constant('\\yii\\web\\View::POS_HEAD')) }}
And, you can also call registerJs inside the Yii2 controller, which I think it makes the code cleaner
$this->getView()->registerJs('$("span.show").replaceWith(\'<div class="show">\');', \yii\web\View::POS_HEAD);
Your example is copied from the smarty section not the twig. There are some examples in the twig section that you might want to try.

How do I display a block in Twig if there is data passed to it?

I have a page in a project I'm working on where I'm having a bit of a problem with.
The page displays two sets of results. The first set of results displays the information for the a project that has just been created. The second set of results displays all the reviews and revisions for this project. This is where I'm having the problem.
When the project is created, there are no reviews created. However, because I have set the page to display reviews related to the selected project, it is expecting to retrieve reviews for the selected project. As there are no reviews to retrieve (because they haven't been added yet) the page fails and gives an ambiguous 500 error.
Now, I have seen somewhere that Twig allows if statements.But, I can't seem to work out how to make this work in my project.
Does anyone know how you would use an if statement in Twig in conjunction with a MySQL result?
I've "Twigged" it (I'm here all week)
{% if reviews == null %}
Stuff to display if the result is null
{% else %}
Stuff to display if the result isn't null
{% endif %}
The loop is then inserted inside this if statement.

Resources