Directory Path in Twig - symfony

In my twig file i have a JS method that needs a path to flash files located in web/bundles/bm/swf/.. I don't need to load a specific file just point the method to that path.
{% block javascripts %}
{{parent()}}
<script type="text/javascript" src="{{ asset('bundles/bm/js/soundmanager2.js') }}"></script>
<script>
soundManager.url = ''; < needs the path to web/bundles/bm/swf/
soundManager.onload = function() { }
</script>
{% endblock %}
Any idea?

{% block javascripts %}
{{parent()}}
<script type="text/javascript" src="{{ asset('bundles/bm/js/soundmanager2.js') }}"></script>
<script type="text/javascript">
soundManager.url = '{{ asset('bundles/bm/swf/')}}';
soundManager.onload = function() { }
</script>
{% endblock %}

Related

handlebars template can't fill with data

I need to output data and fill my template throught custum function
this is my imports
<script src="{{ asset('bundles/.../handlebars/handlebars.js') }}"></script>
<script src="{{ asset('bundles/.../handlebars_helpers.js') }}"></script>
this is my template into twig page
<script id="di_template" type="text/x-handlebars-template">
{% verbatim %}
<h1>Title </h1>
Invoice {{ invoice.invoice_number }}
{% endverbatim %}
</script>
this my build Template Function
function buildTemplate(){
context = {
invoice: {
invoice_number: 'val1',
invoice_date: 'val2',
invoice_due_date: 'val3',
invoice_from_company: 'val4'
}
};
template = $('#di_template').html();
template_compiled = Handlebars.compile(template);
theCompiledHtml = template_compiled(context);
$invoice_preview.html(theCompiledHtml);
}
this my result

using a dynamic class for the body tag

in my base.html.twig my body looks like this:
<body>
{% block header %}{% endblock %}
{% block body %}
{% endblock %}
{% block footer %}{% endblock %}
{% block javascripts %}
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="{{ asset('/js/bootstrap.min.js') }}"></script>
<script src="{{ asset('/js/remodal.min.js') }}"></script>
<script src="{{ asset('/js/script.js') }}"></script>
{% endblock %}
</body>
Since my files homepage.html.twig and subpage.html.twig extend the file base.html.twig, I would like to pass a class to the body tag.
E.g. in my file homepage.html.twig I want it to be <body class="home"> and in my subpage.html.twig file I want it to be <body class="subpage">.
Is it possible to simply pass a variable or something like that or would it be necessary to use a block for that?
I found out that the easiest way is to simply define a block like this:
<body class="{% block body_class %}sub_page{% endblock %}">
Then only in homepage.html.twig using:
{% block body_class %}homepage{% endblock %}

Symfony2, Can't display calendar of adesigns CalendarBundle

I'm trying to display the calendar of adesigns CalendarBundle
https://github.com/adesigns/calendar-bundle
I followed the "Installation" and "Usage" parts, but nothing shows up
{% extends '::base.html.twig' %}
{% block title %}calendar{% endblock %}
{% block body %}
{% include 'ADesignsCalendarBundle::calendar.html.twig' %}
{% endblock %}
I don't know what to do, do I have to write something in the controller? create a bundle?
It works now
I added:
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>
It's important about this problem the position of the links must be in the layout.html.twig or in this example in base.html.twig.
In the documentation the calendar-bundle only say the requirement of installation FOSJsRouting but they not say configuration this links is obligatory:
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>
For this job I work with Symfony 3
Regards

Variable " asset_url" does not exist

Not sure what's wrong but I can't include javascript and/or stylesheet files. Using Symfony3
{% block javascripts %}
{% javascripts
'#AppBundle/assets/js/jquery.js'
'#AppBundle/assets/bootstrap/js/bootstrap.min.js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
getting this error:
Variable " asset_url" does not exist in security/login.html.twig at line 32
Overall, the code should probably look like this:
{% block javascripts %}
{% javascripts
'#AppBundle/assets/js/jquery.js'
'#AppBundle/assets/bootstrap/js/bootstrap.min.js'
output='js/compiled/app.js' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
If it doesn't work then make sure that path to these files is correct.

symfony2: including inline javascript

I've added some javascript code below to show some photos using a slide show jquery plugin.
//parent template
{% block javascripts %}
<script src="{{ asset('http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js') }}" type="text/javascript"></script>
<script src="{{ asset('bundles/canalonesfrontend/js/slides.min.jquery.js') }}" type="text/javascript"></script>
{% endblock %}
//child template
{% block javascripts %}
{{ parent() }}
$(function(){
$("#slides").slides();
});
{% endblock %}
The problem: the code is shown in the web page directly:
Some content
$(function(){ $("#slides").slides(); });
you have to wrap with <script></script> tag around your code
//child template
{% block javascripts %}
{{ parent() }}
<script type="text/javascript">
$(function(){
$("#slides").slides();
});
</script>
{% endblock %}
I hope you understood the problem!

Resources