I have a setup a simple twig template which is used to display a simple menu. I have images in here that are static and I would like use the template directory path for the image src. However when I use {{theme.link}} it appears blank. Perhaps I'm referencing something incorrectly. Code below:
<?php
$context['menu'] = new TimberMenu('main-nav');
Timber::render('templates/menu.twig', $context);
?>
and the twig template below:
<ul>
{% for item in menu.get_items %}
<li class="{{item.classes | join(' ')}}">
{{item.title}}
</li>
{% endfor %}
</ul>
<img src="{{theme.link}}/assets/images/test.png" alt="">
I understand that I can pass in the directory to the context but I'm curios as to why the built in function isn't working. Probably something simple. First time looking into twig so still getting used to it. Any help greatly appreciated! Thanks
#verdond2: In order to use the {{theme}} object (and its properties) you need to start with the default Timber context in your PHP file...
<?php
$context = Timber::get_context();
$context['menu'] = new TimberMenu('main-nav');
Timber::render('templates/menu.twig', $context);
?>
Related
Is it possible to remove the logo header shown in the web-based output of NelmioApiDocBundle? And if so, how?
I would like to include the output of this endpoint (/api/doc if you're using the default settings) in an existing website. However, that site already has a header, and adding a second header would be a poor user experience IMHO. But I can't find any place in the documentation that describes how to exclude this gloriously 1990s-style piece of web design.
I've provided a screenshot of the default config below, which is what I get when viewing the docs in a tool like Insomnia. As you can see, the green header is covering the page as it scrolls.
Yes, you can do it.
You should override bundle template, say steps bellow :
1 - Create template file in 'templates/bundles/NelmioApiDocBundle/SwaggerUi/index.html.twig'
2 - Put the content bellow :
{% extends '#!NelmioApiDoc/SwaggerUi/index.html.twig' %}
{% block header %}
<a style="text-decoration: none;" id="logo" href="#" target="_blank">
<h1 style="color: black">You personal title</h1>
</a>
{% endblock %}
That's it , we done
I've now tested the answer provided by #Abdelhak-ouaddi, and while it's correct, I realize that I didn't ask the question clearly enough to get the answer I needed. The provided answer does work for changing the CONTENTS of the green header, but it doesn't remove the header entirely.
See this page at the NelmioApiDocBundle GitHub page for details on how to customize the twig template. The actual default template is located in the repo here.
What I found is that you can easily override the {% header %} twig block to change the contents of the header as the accepted answer states, but since the {% header %} tags are INSIDE the <header> HTML tags, doing this can't remove the header entirely.
In order to remove the ENTIRE header, you need to follow the steps in the accepted answer... but instead of just overwriting the {% header %} block, paste in the entire contents of the index.html.twig file. Then, completely delete the <header> HTML tags and everything inside them (including the {% header %} twig tags).
As a bit of extra credit: removing the header entirely leaves you with a large margin above the page title. You can get rid of that by adjusting the <body> tag in your new index.html.twig file to:
<body style="margin: 0px;">
I'm sure there's a cleaner way to do that, but... that's for another ticket.
I have a do_shortcut and I need to embed it in a twig template. I tried by coping the code in a php file my-code.php:
<?php do_shortcut('[my-code]'); ?>
Next, in the twig page over.twig:
{{ include ('options/my-code.php') }}
/* I also tried */
{% php %}
<?php do_shortcut('[my-code]'); ?>
{% endphp %}
But doesn't work. Any suggestion? Thanks.
You can't do that, you should create a twig extension and transform the php function into a twig function: http://symfony.com/doc/current/cookbook/templating/twig_extension.html
About the include part, create a my_code.html.twig file at app/Resources/views/my_code.html.twig and copy-paste your code from my-code.php
Then you can include that code anywhere like :
{% include 'my_code.html.twig' %}
EDIT: tested and working in symfony3
Try this code:
{{ wp.do_shortcode('[shortcode]')|raw }}
What is the best way to add a link to a Magento template file?
At the moment I'm using the below method but I'm wondering if there is a better way which calls a native magento method?
<?php echo $this->__('About Us'); ?>
?php echo $this->__('Shopping Bag'); ?>
I know you can use <img src="<?php echo $this->getSkinUrl('images/test.png');?>" /> to get an image url, is there something similar for links?
Use this code to get URL in template file
$this->getUrl('module/controller/action'); //for controller pages
$this->getUrl('', array('_direct'=>'some-url-key')); //for cms pages
$this->getUrl('module/controller/action', array('_query'=>'a=2&b=5')); //for query params
$this->getUrl('', array('_direct'=>'some-url-key', '_query'=>'a=2&b=5')); //for query params
Refer this link https://magento.stackexchange.com/questions/14443/magento-get-store-url-in-cms-page
You can put the links on the static block and then call that static block in your template file.
In Static block code will be
<ul>
<li>About Us</li>
<li>Customer Service</li>
</ul>
I have been stumped for a couple days and I am seeking some direction.
I am attempting to call an image path stored in database to twig file in order to display said image. The twig example below, I am expecting the same image to be displayed twice. When inspecting the rendered html, the variable passes the path from the database, but the first image is not displayed.
From controller:
'logo' => $vendor->getLogovendors()
From database column logoVendors:
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
From twig:
<div class="container">
{{logo | raw}}
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
</div>
I am new to Symfony and its asset management. Any help or prodding in the right direction would be appreciated.
You should normally store only the path to the image in your database!
If logo was the variable you pass to the template holding the image path bundles/loginlogin/img/fs_logo_large.png you could simply include it using twig's asset function like this:
<img src="{{ asset(logo) }}"/>
what you're trying to do ( evaluating a twig function inside a string ) can be solved aswell...but i don't recommend it.
If you want to store the complete code including {{ asset() }} in your database you need to make twig evaluate the code inside the string.
This means twig shall execute the code inside the string instead of just printing it.
This can be achieved using the evaluate filter from this answer.
The final result would then be:
{{ logo |evaluate |raw }}
I'm looking to get started with Twig, but having a real headache getting the {% block %} to work at all - I feel there must be something very obvious I am missing.
My index.php loader looks as follows:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once( "Twig/Autoloader.inc.php" );
Twig_Autoloader::register();
$twig = new Twig_Environment( new Twig_Loader_Filesystem("templates"));
$vars = array (
"user" => array(
"name" => "Joe Bloggs"
),
"title" => "My website"
);
$tpl = $twig->loadTemplate("index.html");
echo $tpl->render($vars);
?>
A simplified version of the index.html in /templates looks like this:
<!doctype html>
<html>
<body>
Hello World!
{% block navigation %}Test{% endblock %}
</body>
</html>
And the navigation.html in /templates looks something like this:
{% extends "index.html" %}
{% block navigation %}
<!-- Navigation -->
<nav>
Some navigation
</nav>
{% endblock %}
So far as I understood it, this should be a basic working example of the blocks feature. The other aspects of Twig seem to be working just fine for me, and no errors are being reported. Indeed, the page successfully prints "Test".
Should I be explicitly pointing to the navigation.html file somewhere, or does Twig automatically load all the files in the /templates folder?
The error: you're rendering your index template instead of the navigation one.
In the index template, the navigation block contains "Test", so your output is correct. If you render navigation.html, you'll get your html content from index.html and the navigation block from the navigation template (the only thing it overrides).
You always need to render the template you wish to output. One may be extended by many (for example, your layout may be extended by all your actions' templates).