How to display single column data in inside of #php in Laravel blade file - laravel-blade

I can display id data as this code.
{{ $image->id }}
however this below code didn't work.
Could you teach me how to write correct code in #php please?
#php
if({{$image->id}} = "6"){
echo "ID is six";
}
#endphp

You can use variables without {{ }} inside #php
You need to use == for comparison
No need to add quotes to id.
try this
#php
if($image->id == 6){
echo "ID is six";
}
#endphp

Related

Timber how to implement get_current_url wp

Ive been using Wordpress for more than a year now. But I was stuck with the implementation of Timber twig framework get the current URL. I tried these codes below codes but no luck,.
{{ site.url.current }}
{{ app.request.getRequestUri() }}
Twig templates engine: get current url
Have you tried:
URLHelper::get_current_url()
Doc: https://timber.github.io/docs/reference/timber-urlhelper/#get_current_url
So, you should be able to feed this as a variable into your template.
Or if you want to get a step further and extend Timber's Twig i.e. creating a filter or function like:
$twig->addFilter(new \Twig_SimpleFilter('is_current_url', function ($link) {
return (URLHelper::get_current_url() == $link) ? true : false;
}));
Which should bring things down to:
{{ 'http://example.org/2015/08/my-blog-post' | is_current_url }}
BTW: Internally, get_current_url() returns: $_SERVER['HTTP_HOST']/$_SERVER['SERVER_NAME'] + $_SERVER["REQUEST_URI"]
Adding ['current_url'] in functions.php under add_to_context function worked for me:
public function add_to_context($context)
{
// $context['foo'] = 'bar';
$context['current_url'] = Timber\URLHelper::get_current_url();
$context['site'] = $this;
return $context;
}
You then will be able to use it globally in your twig templates:
<pre>
Current Url: {{ dump(current_url) }}
</pre>

Remove the '\' from a string in twig

I have this text :
Ch d\`Arnb
Now I want to remove all \ from string if exist. I try a lot of solutions, one of this : {{ item|replace({'\/': ''}) }}. But without success. What can I try next?
There is an error in the replace parameter. You are presently trying to remove the / char. Try this:
{% set item = 'Foo\Bar' %}
{{ item|replace({'\\': ''}) }}
Output: FooBar

Converting spaghetti code to Twig

Need to convert script with "spaghetti code" to Twig. Read Twig documentation and got basics working. However, I need advice on how to do everything properly, so no re-conversion is needed later. Let's say current script looks following:
file index.php:
<?php
$page_message="do it";
function display_dropdown($max)
{
for ($i=0; $i<$max; $i++)
{
echo "<option value='$i'>Option $i</option>";
}
}
?>
<!DOCTYPE html>
<html>
<body>
<h1><?php echo $page_message; ?></h1>
<form method="post" action="<?php echo basename($_SERVER["SCRIPT_FILENAME"]); ?>">
<select name="whatever"><?php display_dropdown(10); ?></select>
<input type="submit" value="go">
<?php include("footer.php");?>
</body>
</html>
footer.php looks:
<?php
$footer_text="blah blah";
?>
<footer><?php echo $footer_text; ?></footer>
As far I understand, my index.php should look like this when converted to Twig:
<?php
$page_message="do it";
function display_dropdown($max)
{
for ($i=0; $i<$max; $i++)
{
echo "<option value='$i'>Option $i</option>";
}
}
$twig_params_array=array("page_message"=>$page_message, "footer_text"=>"blah blah");
require_once("../lib/Twig/Autoloader.php");
Twig_Autoloader::register();
$loader=new Twig_Loader_Filesystem("templates");
$twig=new Twig_Environment($loader);
echo $twig->render("index_template.html", $twig_params_array);
?>
Then I should create index_template.html and footer_template.html (or whatever) with following code:
index_template.html
<!DOCTYPE html>
<html>
<body>
<h1>{{ page_message }}</h1>
<form method="post" action="{{ _self }}>">
<select name="whatever"><?php display_dropdown(10); ?></select>
<input type="submit" value="go">
{{ include('footer_template.html') }}
</body>
</html>
footer_template.html
<footer>{{ footer_text }}</footer>
If I understand right, it's also possible to "include" functions in Twig templates (with some tweaking in templates), so I don't need to rewrite existing PHP functions like display_dropdown(). Because dropdown is not displayed at the moment...
The thing that concerns me is array with variables (passed to Twig render function). Do I miss something, or is it really needed to manually define each variable (like $page_message and $footer_text) before Twig can work?
It seems like a lot of work to do, because in "spaghetti code" if I define variable somewhere, I can access it at any time just by using echo function. Now, it looks I need to view every single variable that exists in PHP code and manually pass it to Twig parameters array. Really?
Since no solution was provided, I found it myself. All you have to do is use such a line of code, so all the variables you have in your PHP file (counting included files) become available in Twig templates, and you don't need to re-write anything when new variables are added to PHP file later (they also automatically become available in Twig):
echo $twig->render("template_file_name.extension", get_defined_vars());
Mindaugas, just changing the technology you use to render views is not going to make the code less tangled.
I think you should go one step further and divide responsibilities, by using an MVC framework, like Symfony, which uses Twig as its default templating language.
Good luck!

How can I load css file using source function of twig?

I'm twig beginner, I Want to get the CSS file content and not the path to be clear, this file is located in:-
Bundle/Ressources/public/css/'~var~'/main.css
var is a dynamic variable, so I used the source function of twig, but it seems that I don't point to the right path. Here is my code:
<style>
{{ source('/Bundle/Resources/public/css/'~var~'/main.css', ignore_missing = true) }}
</style>
Any help please.
You are right. You are not using correct notation for the path.
In your case, if your bundle is named FooBundle, you should use:
{{ source('#FooBundle/Resources/public/css/'~var~'/main.css', ignore_missing = true) }}
If your file's location was inside Resources/views/ for example Resources/views/foo/bar.html.twig you could use even shorter variants:
{{ source('FooBundle:foo:bar.html.twig', ignore_missing = true) }}
or:
{{ source('#Foo/yourTemplate.html.twig', ignore_missing = true) }}
See also: http://symfony.com/doc/current/templating/namespaced_paths.html

Symfony2 : Auto htmlentities using Twig

I'm displaying some variable retrieved in my database using Twig :
<p>{{ my_variable }}</p>
The thing is this variable may contain html tags, such as "<br />".
Twig seems to automatically call some htmlentities-like function when displaying variables.
Is there any way to disable it so that when I display a variable containing "Hello<br />world !" I get :
Hello
world !
rather than :
Hello<br />world !
Thanks
Use {{ my_variable|raw }} to prevent my_variable from being automatically escaped.
See Twig documentation: http://twig.sensiolabs.org/doc/filters/raw.html
Try using this
{% autoescape false %}{{ my_variable}}{% endautoescape %}
even better: {{ '<br />|raw('html') }} to avoid unescaping other sensible stuff.
If you just want to use linebreaks in the text stored in your database but don't care to use html , you can also use the nl2br filter as in {{ var|nl2br }}. Allows you to use string linebreak character \n in your text. Filter converts it to <br/>

Resources