Symfony2 - How to only show specific elements to ROLE_USER - security

I am trying to hide CRUD elements in TWIG so that it only appears for the specified ROLE_USER.
Right now I am using IS_AUTHENTICATED REMEMBERED which works against anonymous users but other logged in users are still able to see this.
{% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
<li>
<a href="">
Create a new entry
</a>
</li>
{% endif %}
I want to only show this to the specific ROLE_USER that I have set in the access_control in security.yml and the controller. For instance the code above should only be shown to ROLE_USER1.
What is the command to do this in Twig?

Use is_granted('ROLE_USER1')
{% if is_granted('ROLE_USER1') %}
<li>
<a href="">
Create a new entry
</a>
</li>
{% endif %}

Related

Display product custom field in listing page in shopware 6?

I created a custom field and assigned it to the products and I want to display that custom field value on the listing page. On the description.html.twig page, I add this below variable and the value is showing on the detail page.
{{ page.product.translated.customFields.custom_events_dates }}
The same variable I placed on the box-standard.html.twig, but the value is not showing.
{% sw_extends '#Storefront/storefront/component/product/card/box-standard.html.twig' %}
{% block component_product_box_info %}
{{ parent() }}
<div class="test">
<p>{{ page.product.translated.customFields.custom_events_dates }}</p>
</div>
{% endblock %}
How can I display the custom field value on the listing page?
You need to use product.translated without the page. The page object only contains the product data on a product detail page, on listing pages it is not set.

Digg-style pagination customize in template

I am using django-el-pagination package and trying to implement digg-style pagination on my home template. I am displaying it with..
{% get_pages %}
{{ pages.get_rendered }}
and it is showing like this <1234567> which is fine but
I want to add some css or class to change the way it look.
If you have any other way to achieve this. Please suggest..
please tell how can I customize it.
You can change it to make it looks something like this or like whatever you want:
Here is an example:
{% get_pages %}
<div class="pagination">
{% for page in pages %}
<li class="page-item">{{ page.number }}</li>
{% endfor %}
</div>
You can check the documentation here: https://django-el-pagination.readthedocs.io/en/latest/templatetags_reference.html#get-pages to check all the available options for get_pages

Twig Template Inheritance Issues

I'm trying to setup a clean structure for my Craft CMS install, but having issues with some very basic twig and a nice way to structure my things.
See this gist - why would my Navbar not show in this example?
Even better, I would like to structure my site like this - but it doesn't show anything? - another gist
Am I mixing the extend/block pattern the wrong way somehow? This feels so very basic and thus it's extremely frustrating. I would appreciate any help.
In your first gist, in index.twig, you extend base.twig correctly. However, you don't specify the contents of the navbar block, so the block defaults to the empty content that is set in base.twig.
You can change base.twig to include navbar.twig using the include function:
...
<div id="content">
{% include "navbar.twig" %}
{% block content %}{% endblock %}
</div>
...
You should also not extend base.twig in navbar.twig to prevent infinite recursion (base includes navbar, which extends base, which includes navbar, ...).
If you want the navbar to be overridable in index.twig and other files, you can retain the navbar block in base.twig and set its default contents to include navbar.twig:
...
<div id="content">
{% block content %}
{% include "navbar.twig" %}
{% endblock %}
{% block content %}{% endblock %}
</div>
...
Then, in index.twig, if you omit {% block navbar %}{% endblock %}, the navbar will use the contents from navbar.twig. Or you can also override the navbar block's contents:
{% extends "base" %}
{% block navbar %}
<h2>Overridden navbar</h2>
{% endblock %}
{% block content %}
<h2>Main content</h2>
{% endblock %}
In your second gist, in index.twig, you can change {% block head %}{% endblock %} to {% include "head.twig" %} and so on. In head.twig and other files, you should again not extend index.twig to prevent infinite recursions.
I recommend taking a look at Twig's documentation about the extends tag to see how blocks and templates actually work. In your two gists, blocks and templates are not used correctly.

lektor menu with links to child pages only, parents toggle submenu, without links

I need to generate a menu that lists all pages (like the sitemap sample) but if a page has childs it should become only a toggle for its submenu, not linking to any page. Only the innermost childs will be pages.
E.G.:
about (link)
portfolio (no link - only toggle)
work01 (link)
work02 (link)
contacts (link)
As I said, the summentioned sample creates and links a page also for "portfolio".
My site is here andrearicci.it (subpage) and the menu item "projects" now toggles the submenu, but I can't get rid of its link - so I had to format an unwanted 'projects summary' page.
On that page the menu is
<nav>
<ul class="projlist">
{% for project in site.get('/projects').children %}
<li{% if this == project %} class="active"{% endif
%}>{{ project.name }}</li>
{% endfor %}
</ul>
</nav>
I wish an automatic solution, that could work also adding other page groups (e.g. paintings/web design/whatever) without the need of changing it every time.
Is it maybe more a question about Jinja other than Lektor?
Thank you for any help.
A.R.
Idea: just test if the page contains children, if so - do not link the page.
The test should be as easy as
{% if project.children %}
{# no link #}
{% else %}
{# link #}
{% endif %}

Octopress site.navigation

I have this thing in my Octopress blog:
<ul class="nav">
{% for link in site.navigation %}
<li {% if page.url == link.url %}class="active"{% endif %}>{{ link.text }}</li>
{% endfor %}
</ul>
What this:
{% for link in site.navigation %}
I checked _config.yml, but didn't find navigation definition. I searched the whole project for the string "navigation", without avail. Can someone please help me understand what this {% for link in site.navigation %} is, and how I can change it?
So, kikito thinks he's correct. Well, yes. Spot on, my friend.
I think the navigation yaml could go inside an _include called "navigation" or something similar, but I haven't tried using yaml inside those so I don't know whether it will work. In my case, since I've got a multi-lingual site, it's easier to have everything inside config (missing translations are easier to spot)
Bascially, go to navigation.html inside the _includes directory, and you may find the source where site.navigation is populated from. In my case:
{% include custom/navigation.html %}
<ul class="nav pull-right">
{% if site.github_user %}
<li><i class="icon-github-sign social-navbar"></i></li>
{% endif %}
{% if site.bitbucket_user %}
<li><i class="icon-bitbucket-sign social-navbar"></i></li>
{% endif %}
...
</ul>

Resources