Unescaped pug attribute not processed by laravel blade - laravel

I'm using Laravel Pug https://github.com/BKWLD/laravel-pug, and the file is named *.pug.blade.php. I'm trying to set an attribute value.
| <?php $x = "any"; ?>
div(class!="{{ $x }}")
| {{ $x }}
the output html is
<div class="{{ $x }}">any</div>

Interestingly, in attribute, the php variable is accessed with no < ? php ? > or {{ }} enclosures, just like javascript variables
| <?php $x = "any"; ?>
div(class=$x)
| {{ $x }}
the HTML output
<div class="any">any</div>
Also, the dollar sign can be omitted, and interpolation can be used
| <?php $x = "any"; ?>
div(class = x + "test")
| {{ $x }}
the HTML output
<div class="anytest">any</div>

Related

How can we fetch images from database to dashboard using CodeIgniter 3

Image path is getting stored in the database and file is getting uploaded in the upload path but the image is not displaying in the tabular format.
Here is the code for the view:
<tbody>
<?php
$query = $this->db->query("SELECT * FROM banner_mgmt LIMIT 1");
// $url = base_url().
foreach ($query->result() as $row)
{
echo "<tr>";
echo "<td>$row->id</td>";
echo "<td>$row->banner_name</td>";
echo "<td>".'<img src="base_url().$row->banner_image" style="height:150px; width:150px;" alt="not found">'."</td>";
echo "<td>$row->banner_image</td>";
echo "</tr>";
}
?>
</tbody>
Assuming that base_url().$row->banner_image does actually result in a correct path/image name...
You need to check your string concatenation.
What you have... (overly complicated)
echo "<td>".'<img src="base_url().$row->banner_image" style="height:150px; width:150px;" alt="not found">'."</td>";
What you should have...
echo '<td><img src ="'.base_url().$row->banner_image.'" style="height:150px; width:150px;" alt="not found"></td>';
You need to think about how you want the string to turn out, which helps you determine when and where to use ', " and . properly.
Here I am wrapping the string using ' (single quotes), which allows me to use " (double quotes) in the HTML properties without much effort.
Read up on how to use base_url() in the Codeigniter user guide...
So you could have
echo '<td><img src ="'.base_url($row->banner_image).'" style="height:150px; width:150px;" alt="not found"></td>';
You could even use " (double quotes) which evaluate php but you will need to escape any 'internal' double quotes you want to display.
echo "<td><img src =\"base_url($row->banner_image)\" style=\"height:150px; width:150px;\" alt=\"not found\"></td>";
So, you need to read the answer at What is the difference between single-quoted and double-quoted strings in PHP?

Using for loop value in foreach loop

I would like to use a value inside a for loop in foreach in blade file. Does anyone know how to do this? I would like to do something like this:
#for ($i = 0; $i < 10; $i++)
#foreach($meals->where('meal_category_id', {{ for loop value }}) as $meal))
#endforeach
#endfor
Thanks for help :)
Did you try:
#for ($i = 0; $i < 10; $i++)
#foreach($meals->where('meal_category_id', '=', $i) as $meal))
{{ $meal }}
#endforeach
#endfor

How can I add operator ternary on section blade laravel?

I try like this :
#section('title', 'Search' . '"' . empty($search) ? '' : $search . '" | Myshop')
On my view blade laravel
But it does not work
I want if $search exist, the result like this :
Search "test" | Myshop
For example $search = test
IF $search not exist, the result like this :
Search "" | Myshop
How can I do it?
to display the results that you want you can use this:
If condition appoarch
#section('title')
Search "#if(isset($search)){{$search}}#endif" | Myshop
#endsection
or
Ternary approach
#section('title')
Search "{{isset($search) ? $search : ''}}" | Myshop
#endsection
instead your existing code.
Assuming that your $search will always be defined... this should work. If it's not defined, then use isset instead of empty.
#section('title', "Search \"" . empty($search) ? null : $search . "\" | Myshop")

How do I access variables under liquid filter argument?

module MyFilter
def all_caps(input)
input.upcase
end
end
Liquid::Template.register_filter(MyFilter)
template = Liquid::Template.parse(" {{ 'hi john' | all_caps }} ")
template.render
# => " HI JOHN "
template = Liquid::Template.parse(" {{ 'hi {{name}}' | all_caps }} ")
template.render('name' => 'john')
# => " ' | all_caps }} "
How do I fix this? No mentions in the official documentation.
Note: I have used all_caps example just for the simplicity. I want to achieve something complex.
I did figure out a way to do that. Would appreciate if there is any other solution.
template = Liquid::Template.parse("{% capture some_text %} Hi, {{name}} {% endcapture %} {{ some_text | all_caps }}")
template.render('name' => 'john')
Hope this helps.

How to preserve gettext() translations?

Suppose I have a example.php file like that:
<p>
<?php echo _('Hello world') ?>
</p>
<p>
<b><?php echo _('the end') ?>
</p>
If I extract strings:
xgettext example.php
I get a messages.mo file, that I can open with poedit, translate, create a .po file, etc. That's ok, the problem is when I edit my original and already translated example.php:
<p>
<?php echo _('Hello world') ?>
</p>
<p>
<?php echo _('new string') ?>
</p>
<p>
<b><?php echo _('the end') ?>
</p>
I've added a new string and if I execute xgettext again I get a messages.mo file where all strings are empty, so I have to use poedit and translate again all strings. How can I re-use my previous translations?
You can merge two po files together with msgmerge. If the source string is unchanged the merge should work perfectly, if it has changed you obviously may have to do some work to get things translated again, and of course you will have to translate any entirely new strings.
msgmerge -o results.po my_existing_translations.po untranslated_xgettext_output.po

Resources