I seen similar questions here but none of them not working for me.
In addition these are old questions for older symfony2 version (different parameters). This can also happen in symfony3.
I need query version parameter to my production JS and CSS assets to avoid using old files. (by users ofc) For now generated asset looks like this:
<script type="text/javascript" src="/js/56a0cb5.js"></script>
In my template:
{% javascripts '#XBundle/Resources/public/js/jquery-1.9.1.min.js'
'#XBundle/Resources/public/js/ga.js'
'#XBundle/Resources/public/js/somejs.min.js'
'#XBundle/Resources/public/js/main.js' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
And now also I added some lines in configuration file according to documentation:
framework:
assets:
version: 'v2'
version_format: '%%s?ver=%%s'
but still my asset url looks the same.
How can I achieve this?
<script type="text/javascript" src="/js/56a0cb5.js?ver=v2"></script>
Thank you
You could declare version in templating section of framework like this:
framework:
templating:
engines: ['twig']
packages:
js:
version: 'v2'
version_format: '%%s?version=%%s'
In template:
{% javascripts '#XBundle/Resources/public/js/jquery-1.9.1.min.js'
'#XBundle/Resources/public/js/ga.js'
'#XBundle/Resources/public/js/somejs.min.js'
'#XBundle/Resources/public/js/main.js' package='js' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
Related
To my understanding, by using twig's extends method, it should be possible to add code to the footer from within a template that is included anywhere else.
Like if we render the main page and have a <form> there in a template edit_form.html.twig and we know we need special javascript here (only for that form) so we decide to add this javascript tags in the footer at the end of the page like this:
edit_form.html.twig
{% block edit_form %}
<form id="editForm">
<!-- form html here -->
</form>
{% endblock edit_form %}
{{ include('edit_form_js.html.twig') }}
We want this javascript to be at the end of the page:
edit_form_js.html.twig
{% extends "footer.html.twig" %}
{% block js %}
{{ parent() }}
<script>
// special js code only needed for the <form>
</script>
{% endblock js %}
footer.html.twig
{% block js %}
<script src="jquery.js"></script>
{% endblock js %}
But doing this does not add the custom form javascript at the end after the jquery inclusion but results in the block js being twice on the page (after the form and in the footer)
Is my understanding of twigs extends wrong?
Q: Is there any way to add code from anwywhere to the footer?
Included templates don't know anything from the template it's being called from. There for an included template cannot alter a block from that said template. You might want to have a look at the deferred extension
I am trying to include my CSS and JS files on my Twig template like this
{% block stylesheets %}
<link href="{{ asset('bootstrap.min.css') }}" rel="stylesheet" />
{% endblock %}
{% block javascripts %}
<script src="{{ asset('jquery-3.2.1.slim.min.js') }}"></script>
<script src="{{ asset('popper.min.js') }}"></script>
<script src="{{ asset('bootstrap.min.js') }}"></script>
{% endblock %}
I get an error on Slim like this
Unknown "asset" function.
How do I include my CSS and JS files in Twig via Slim?
There’s no such function by default. Either install an Asset extension or hardcore links by yourself.
I'm looking for alternatives to Jade templates in express 4.x because I really don't like Jade's syntax. I'm tending towards EJS, because it's basically just HTML on steroids.
However, one really nice feature of Jade templates is the ability to use layouts. I've found https://www.npmjs.org/package/express-ejs-layouts, but it seems to be made for express 3 and its build is failing :/.
I also found https://www.npmjs.org/package/ejs-mate which is made for express 4.x but it only seems to support a single content block (body).
I would like to have something like this:
layout.something:
<html>
<head>
<% block styles %>
<% block scripts %>
</head>
<body>
<% block body %>
</body>
</html>
index.html:
uses layout "layout.somehing"
scripts:
<script src="my_custom_script.js"></script>
styles:
<link rel="stylesheet ...></link>
body:
<h1>This is my body!</h1>
So that this yields:
<html>
<head>
<link rel="stylesheet ...></link>
<script src="my_custom_script.js"></script>
</head>
<body>
<h1>This is my body!</h1>
</body>
</html>
Does anyone know an engine that is capable of that besides Jade?
You can try express-handlebars, it supports layout and partial views.
I have the following template:
<!DOCTYPE HTML>
<html>
<head>
<link href="css/style.css" rel="stylesheet"/>
<script type="text/javascript" src="/js/libs-0001.js" async="async"></script>
<script type="text/javascript" src="/js/app-0004.js" async="async"></script>
<script>
var name = {literal}<%= name %>{/literal};
var version = {literal}<%= version %>{/literal}};
</script>
</head><body></body></html>
Like in smarty, from php, I want to use {literal} declarations in the template, under the script session.
How to do this in locomotivejs views?
As far as I understand {literal}/{/literal}, everything between those tags isn't interpreted. EJS templates don't have something similar, although there are ways of circumventing that.
One way is to configure EJS to use different open/close tags, described here.
Another way is to use a different templating engine altogether, which isn't too difficult since LocomotiveJS isn't hardcoded to using EJS templates. I like the Swig templating engine myself, which has the {% raw %} tag that seems to do the same as {literal}.
I am working on integrating Assetic into an application that I am building. The application follows the MVC pattern but does not use any frameworks. Twig is used for templating.
I have read the documentation and have looked at a few helpful blog posts.
The application is structured like this:
application
twig_templates
js
css
public_html
js
css
images
The twig templates are rendered and then returned by the application, so that is quite straight forward. The js and css folders underneath the application folder would hold the raw javascript and css files.
My plan is that combined and minified js and css files would be stored in the js and css folders under the public_html folder.
Since I don't plan to optimize any images using assetic, they will be placed in the images folder underneath public_html directly.
Now the question:
I understand that by using assetic from twig, combined and minified assets can be exported to my public_html directories:
{% stylesheets '/path/to/sass/main.sass' filter='sass,?yui_css' output='css/all.css' %}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}
In this case is the css/all.css file regenerated everytime that template is rendered?
To make the application portable (it could be hosted using a virtual host, or perhaps even in a folder) and to ensure things work in a site with routed URLs like http://site.com/controller/action, does assetic provide anyway "rewrite" URLs for images into absolute ones (i.e. images/a.jpg into http://mysite.com/images/a.jpg)?
Does assetic work when using nested templates?
parent.twig:
<!DOCTYPE html>
<html>
<head>
{% block head %}
<link rel="stylesheet" href="mainstyle.css" />
<title>{% block title %}{% endblock %} - My Webpage</title>
{% endblock %}
</head>
<body>
</body>
</html>
child.twig:
{% extends "parent.twig" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ parent() }}
<link rel="stylesheet" href="childstyle.css" />
{% endblock %}
In this case, how can I let assetic know that it should combine childstyle.css and mainstyle.css and serve it as 1 file?
as for this answer it is not possible to combine inherited stylesheets from parent with child's
Combining Assetic Resources across inherited templates