I have a situation like this in my twig file:
{% javascripts
'js/functions.js'
'js/plugins.js'
'js/editor.js'
'js/calendar.js'
%} [...]
And I would love to comment some scripts out using
{% javascripts
{# 'js/functions.js' #}
'js/plugins.js'
'js/editor.js'
'js/calendar.js'
%} [...]
But this isnt allowed, I get an Unexpected character "#" error.
Is there a common practice how to handle this?
That is unsupported twig syntax, obviously. So it's up to you how to hide it. I would do something like this:
{# 'js/functions.js'#}
{% javascripts
'js/plugins.js'
'js/editor.js'
'js/calendar.js'
%} [...]
Related
I am using a Webpack Encore in my Symfony 4 project, and when including the Twig helper for JavaScript in my base.html.twig:
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{% endblock %}
The JavaScript doesn't load, however, when including the exact same Twig helper in one of my templates (index.html.twig), the JavaScript loads in.
So my question is, why does the above Twig helper work in one of my templates, but not in my base template?
What preciel said is true, using a block inside a template that extends another template will overwrite the default code inside that said block.
However there is another solution than just moving the code outside the block in the base template and that's calling the parent() function
main.twig
{% extends 'base.twig' %}
{% block foo %}
{{ parent() }}
Bar
{% endblock %}
base.twig
{% block foo %}
Foo
{% endblock %}
output
Foo
Bar
demo
It's failing because it's inside your javascript block.
Your template extends base.html.twig, so whatever you place within your javascript block in any template will be inside the javascript block.
But if you do the same within base.html.twig it will just be ignored.
Just move {{ encore_entry_script_tags('app') }} out of your javascript block.
base.html.twig
{{ encore_entry_script_tags('app') }}
{% block javascripts %}
{# nothing here, your templates will overwrite it when you extends base.html.twig #}
{% endblock %}
Remember this: If it's inside your layout, which is base.html.twig, then don't place anything inside the {% block %} tags. It will just be ignored.
I apologize if the question was raised somewhere, and I did not notice the decision.
I have the following files:
base.html.twig - which includes css и js.
blocks.html.twig - which includes blocks.
en.html.twig
sp.html.twig
ru.html.twig
blocks.html.twig
<h1>hello</h1>
<h2>holla</h2>
<h3>привет</h3>
Please tell me how to initialize the object (for example blocksLang and each h* block to place in this object), for including in different places. For example:
in en.html.twig
{% extends "base.html.twig" %}
{% include "blocks.html.twig" blocksLang.en %} //and it should render <h1>hello</h1>
in sp.html.twig
{% extends "base.html.twig" %}
{% include "blocks.html.twig" blocksLang.sp %} //and it should render <h2>holla</h2>
in ru.html.twig
{% extends "base.html.twig" %}
{% include "blocks.html.twig" blocksLang.ru %} //and it should render <h3>привет</h3>
Thank you for attention
thanks for checking out my question.
The problem I am having is with Shopify, Liquid, JQuery and Javascript.
I have a JQuery / Liquid slider that has four different categories. NBA, NHL, MLB and MLS. When the page loads..
jQuery(document).ready(function ($) {
$(".ncaa-carousel-filter").addClass("active");
$("#ncaa-carousel").toggle();
This makes it so that no matter what section of the site you are on, (NBA, NHL, etc..) you will still see the NCAA carousel until you click on the appropriate toggle.
var carousel = "-carousel";
var carouselFilter = "-carousel-filter";
$("#carousel-filters").on("click", ".ncaa-carousel-filter", function() {
$("[class*="+carouselFilter+"]").removeClass("active");
$(this).addClass("active");
$("[id*="+carousel+"]").hide();
$("#ncaa-carousel").toggle();
});
I have broken out the sliders and trying to use Liquid to display them.
{% if collection.current_type or collection.handle contains "mlb" %}
{% include 'mlb-slider' %}
{% elsif collection.current_type or collection.handle contains "nba" %}
{% include 'nba-slider' %}
{% elsif collection.current_type or collection.handle contains "mls" %}
{% include 'mls-slider' %}
{% elsif collection.current_type or collection.handle contains "nhl" %}
{% include 'nhl-slider' %}
{% else %}
{% include 'ncaa-slider' %}
{% endif %}
I believe the problem is that the sliders are set to display: none until toggled. So when I go to include them, they aren't showing as they are set to display: none.
If I remove that CSS I end up with 4 sliders on my page as they are all showing.
I think this is a question of how I have it set up, I need some help on the logic.
This fiddle shows most of the code I am working with. http://jsfiddle.net/asx90842/
Maybe I can remove the display: none CSS and use the Liquid if statement I've developed in place of the HTML code you see in the fiddle?
Any help would be great!!
Thanks so much!
First, instead of toggling ncaa at page load, why don't you just set it to active in your html templates? Simply put a .active class and in your stylesheets do .active{display:block} or whatever you like. If you can use data-* attributes in your buttons it will be even better since you will not have to use regex to get a corresponding id.
So your html will be like this
<button class="ncaa-carousel-filter active" data-target="ncaa-carousel">College</button>
<div style="margin-bottom:10px;" class="teams-carousel clearfix active" id="ncaa-carousel">
Second, all five carousel button/div all share the same logic so you can just write one listener for all of them:
$("#carousel-filters").on("click", "button", function() {
$("#carousel-filters button").removeClass("active");
$(this).addClass("active");
$(".teams-carousel").hide();
// Use data attributes to get corresponding div id
var id = $(this).data('target');
$(id).toggle();
});
Lastly, I am not very familiar with ruby/liquid but I bet there's a for loop structure that you can use to write just one block and iterate over it with different values.
One way to approach this would be to assign a liquid variable which could be re-used in the Javascript class/id selectors. I would include the following liquid near the top of the page.
{% if collection.current_type or collection.handle contains "mlb" %}
{% assign sport_league 'mlb' %}
{% elsif collection.current_type or collection.handle contains "nba" %}
{% assign sport_league 'nba' %}
{% elsif collection.current_type or collection.handle contains "mls" %}
{% assign sport_league 'mls' %}
{% elsif collection.current_type or collection.handle contains "nhl" %}
{% assign sport_league 'nhl' %}
{% else %}
{% assign sport_league 'ncaa' %}
{% endif %}
With that included, you'll have a new variable, sport_league representing the common string of characters used to represent any particular sport / carousel. For example, you'll now be able to write the following at the end of your Javascript to display just the carousel that's wanted.
$('#{{ sport_league }}-carousel').show();
This is simply javascript that executes once, using the variable sport_league to negate the display: none CSS and show the appropriate carousel.
I am using Symfony and Twig and the fragment sub-framework more specifically the Internal sub-requests.
In any case when I request a template using a fragment include
{% render url('route_name') %}
and my fragment code looks like this for example
<div>[code here...]</div>
<script>[javascript here...]</script>
how can I get this javascript to load into a {% block %} in my base.html.twig file? If I extend my fragment and put {% extends '::base.html.twig' %} in the header it will include the entire layout of my site. I just want to be able to push the javascript from from fragment out to my base template.
In my base template I have a {% block %} such as this
{% block javascript_footer %}
[it inherits javascript from child templates...]
{% endblock %}
Thanks!
separate your javascript into another file and than include it into your base template and also into your fragment.
base template:
{% block javascript_footer %}
{% include '::javascript.html.twig' %}
{% endblock %}
your fragment:
<script>{% include '::javascript.html.twig' %}</script>
assuming that javascript.html.twig doesnt include <script> tag already
I would like to share a template between AJAX and regualr HTTP calls, the only difference is that one template needs to extend base.html html, while the other dose not.
I can use
{% extends request.is_ajax|yesno:"app/base_ajax.html,app/base.html" %}
To dynamically decide when to extend, but I also need to include {% block 'some_div' %}{% endbock %} tags to tell the renderer where to put my content. The ajax call needs those tags to be left out because jQuery tells it where to put the content with $('somediv').html(response).
Any idea on how to dynamically include those block tags when its not an ajax call?
I've been referencing this question to figure it out:
Any way to make {% extends '...' %} conditional? - Django
Attempt to make it work through an {% if %}:
{% extends request.is_ajax|yesno:",stamped/home.html" %}
{% if request.is_ajax == False%}
{% block results %}
{% endif %}
{% load stamped_custom_tags %}
...
Content
...
{% if request.is_ajax == False%}
{% endblock %}
{% endif %}
but this fails when parser runs into the {% endif %}
Wouldn't this, or some variant, do?
{% if request.is_ajax %}
{# ajax! #}
{% else %}
{% block 'some_div' %}{% endbock %}
{% endif %}
My Solution:
{% extends x|yesno:"stamped/blank.html,stamped/home.html" %}
Where blank.html contains:
{% block results %}{% endblock %}
<!-- to allow for corrected shared rendering
with ajax posts and normal django rendering -->
and home.html is my standard app page with a results block to extend.