Include twig template with values for nested placeholder? - twig

I have a link template:
link.twig:
<a class="something" href="{{ url }}">{{ text }}</a>
And im calling the template and passing it values:
page.twig:
{% include "link.twig" with {'url': 'www.google.com', 'text': 'Search engine} %}
This was working but now I need to have url and text as part of a link object:
<a class="something" href="{{ link.url }}">{{ link.text }}</a>
How can I pass values from page.twig? The following gives me a white screen of death:
{% include "link.twig" with {link.url: 'www.google.com', link.text: 'Search engine} %}

You can directly include an array :
{% include "link.twig" with {link : { url: 'www.google.com', text: 'Search engine'} } %}

Related

How to hide an element only in homepage?

Trying to do "if homepage hide " a button.
Current code is here...
<div class="mobile-bar sticky-bar">
{% if j3.settings.get('mobileCustomMenuStatus1') %}
<a class="mobile-custom-menu mobile-custom-menu-1" href="{{ j3.settings.get('mobileCustomMenuLink1.href') }}" {{ j3.linkAttrs(j3.settings.get('mobileCustomMenuLink1.attrs')) }} style="margin-left:10px; margin-right:-30px">
{{ j3.countBadge(j3.settings.get('mobileCustomMenuLink1.name'), j3.cache.update(j3.settings.get('mobileCustomMenuLink1.total')), j3.settings.get('mobileCustomMenuLink1.classes')) }}
</a>
{% endif %}
{% if j3.settings.get('mobileCustomMenuStatus2') %}
<a class="mobile-custom-menu mobile-custom-menu-2" href="{{ j3.settings.get('mobileCustomMenuLink2.href') }}" {{ j3.linkAttrs(j3.settings.get('mobileCustomMenuLink2.attrs')) }}>
{{ j3.countBadge(j3.settings.get('mobileCustomMenuLink2.name'), j3.cache.update(j3.settings.get('mobileCustomMenuLink2.total')), j3.settings.get('mobileCustomMenuLink2.classes')) }}
</a>
{% endif %}
You can store the current page within a $data variable in your menu controller just like this:
$data['current_url'] = $_SERVER['REQUEST_URI'];
Within your twig file you can check it's value against whatever you want. This can be achived using a small vqmod or ocextension.

Use a twig filter in template_from_string function

I am creating a multilingual website with October CMS, using Rainlab Translate and Vojta Svoboda Twig Extensions plugins.
I'm using the twig function template_from_string to create a link button in one of my template files.
Everything works as expected if I use a |media filter in the link attribute, to get the url of a media file.
But, if I use a |page filter, to get a page url, I get an error for Unknown "page" filter.
<!-- It works: -->
<div>
{% set btn = {
'link': 'foobar.jpg',
'label': 'Where is FooBar »'
} %}
{{ include(template_from_string('<a class="btn btn-promo btn-white move" href="{{ btn.link|media }}" role="button">{{ btn.label }}</a>')) }}
</div>
<!-- It does not work: -->
<div>
{% set btn = {
'link': 'foobar',
'label': 'Where is FooBar »'
} %}
{{ include(template_from_string('<a class="btn btn-promo btn-white move" href="{{ btn.link|page }}" role="button">{{ btn.label }}</a>')) }}
</div>
I'm stuck on this problem and my question is: How can I get both filters work?
Thank you in advance for your help.
I think you are overlooking stuff :)
You can use like this
<div>
{% set btn = {
'link': 'fooba'|page, <-- HERE
'label': 'Where is FooBar »'
} %}
{{ include(template_from_string('<a class="btn btn-promo btn-white move" href="{{ btn.link }}" role="button">{{ btn.label }}</a>')) }}
</div>
you can use filter on main scope and just pass filtered value directly. you do not need to use filters inside template_from_string
if any doubt please comment.

Django NoReverseMatch : Reverse for ‘entry’ not found. ‘login’ is not a valid view function or pattern

I have a wiki application and I am trying to list entries in homepage(index) with namespacing urls but I am getting the reverse for 'entry' not found error for my view function called 'entry'.
urls.py
from . import views
app_name="wiki"
urlpatterns = [
path("", views.index, name="index"),
path("<str:title>", views.entry, name="title")
]
views.py
from . import util
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def entry(request,title):
return render(request, "encyclopedia/entry.html", {"entry_title":title ,
"entry_body": util.get_entry(title)})
index.html
{% block body %}
<h1>All Pages</h1>
<ul>
{% for i in entries %}
<li> <a href="{% url 'wiki:entry' i %}"> {{ i }} </a</li>
{% endfor %}
</ul>
{% endblock %}
If I change wiki:entry to wiki:index I can successfully generate list items and link all of them to homepage. Somehow {% url 'wiki:entry' i %} doesn't retrieve entry view at views.py and use i for the title . Also, if I hardcode url in the template like href="{{i}}" it does also work.
instead of <li> <a href="{% url 'wiki:entry' i %}"> {{ i }} </a</li> use <li> <a href="{% url 'wiki:title' i %}"> {{ i }} </a</li>
since you are using "title" as your url name for the view "entry"

Is it possible to use for loop in the include tag?

{% include './partial/project.twig' with {'status': 'Past Project',
'statusClass': 'past',
'heroImage': "/dist/images/projects/project-south16th.jpg",
'logo': '/dist/images/logo-south16th.png',
'desc': '23 three- and four-bed townhomes',
'address': '15885 16 Avenue, South Surrey',
'showGallery': true,
'galleryID': 'south16th',
'link': '#',
'galleryImages': "
{% for i in range(1, 10) %}
<a data-fancybox='south16th' href='{{ theme.uri }}/dist/images/gallery/south16/{{ i }}.jpg'></a>
{% endfor %}
"
} %}
The above code is not valid because it seems like twig doesn't allow nested tag in the include tag? Or did I do something wrong?
Is there another way to achieve it? I would like to repeat this code X times and pass it to the template:
{% for i in range(1, 10) %}
<a data-fancybox='south16th' href='{{ theme.uri }}/dist/images/gallery/south16/{{ i }}.jpg'></a>
{% endfor %}
To achieve this you would need to switch up to embed instead of include
index.twig
{% embed 'include.twig' with { 'theme': { 'uri' : 'https://www.example.com', 'pictures': 10, }, } %}
{% block pictures %}
{% for i in 1..theme.pictures %}
<li><a data-fancybox='south16th' href='{{ theme.uri }}/dist/images/gallery/south16/{{ i }}.jpg'></a></li>
{% endfor %}
{% endblock %}
{% endembed %}
include.twig
<h1>Include</h1>
<h2>{{ theme.uri }} - {{ theme.pictures }}</h2>
<ul>
{% block pictures %}
{% endblock %}
</ul>
twigfiddle
note: variables you'd define in include.twig will also be available inside the embed

Dynamic title AND content in Jekyll

I have a page whose structure is somewhat like this
---
layout: contents
title: contents
description: Contents of the posts.
permalink: contents/
---
<ul class="posts">
{% for post in site.posts %}
{% if post.categories != "tips" %}
<h2><a class="post-heading" href="{{ post.url }}">{{ post.title }}</a></h2>
<p> {{ post.description }} </p>
<p class="date">{{ post.date | date_to_string }}</p>
<hr class="hr-color"></hr>
{% endif %}
{% endfor %}
</ul >
Currently the URL of this page is set according to permalinks(BASE_URL/contents). I want that when a user clicks on an option( Android,Java,Web are the options) in the previous page, i get the URL of this page as BASE_URL/android or BASE_URL/Java and also display the contents of that category.
Is this possible using jekyll?
Two solutions :
1 - Using a plugin
You can use this category archive generator
2 - Using hand crafted pages
If you cannot use plugin (gh-pages) you can make a page per category, like this :
android.html
---
layout: category
title: Android
permalink: android/
---
_layouts/category.hmtl
---
layout: default
---
<ul class="posts">
{% for post in site.posts %}
{% if post.categories contains page.title %}
<h2><a class="post-heading" href="{{ post.url }}">{{ post.title }}</a></h2>
<p> {{ post.description }} </p>
<p class="date">{{ post.date | date_to_string }}</p>
<hr class="hr-color"></hr>
{% endif %}
{% endfor %}
</ul >

Resources