Handling conditions in blade templates on laravel 4 in a form input - laravel

I would like to test the existence of value in blade on Laravel 4.
Like:
{{ Form::text('name', #if(isset($value)) {{$value}} #endif; }}
I tried this:
{{ Form::text('name', #if(isset($value)) $value #endif; }}

You can do it in one line.
I'd prefer a better approach, using condition?if:else , which also I use in my daily projects:
{{ Form::text('name',isset($value)?$value:'') }}
or even:
{{ Form::text('name',isset($value)?$value:null) }}

Anywhere before that, you can add the line <?php if(!isset($value)) { $value= ''; } ?>. Then you can proceed as usual {{ Form::text('name', $value) }}

Related

Laravel blog rating 2 migrations

i made a blog in laravel and now created a new migration for a rating system. i call it with
#foreach($blogs as $key => $blog)
$blog->title....
#foreach($ratings as $key => $rating)
#if( $rating->blog_id == $blog->id)
<br> rating found!
<br> {{ $rating->rating_total }}
<br> {{ $rating->rating_amount }}
#endif
#endforeach
#endforeach
it works but it doesnt seem right to run a for loop on every blog post in order to get the rating.
how can i solve this better?
thanks
You could use one that show this. Like this: <input id="input-1" name="input-1" class="rating rating-loading" data-min="0" data-max="5" data-step="0.1" value="{{ $post->averageRating }}" data-size="xs" disabled="">
Since you have the relationship setup from Blog to Rating you can eager load the 'rating' relationship for each Blog post:
$blogs = Blog::with('rating')->...->get();
Then in Blade when you are looping your $blogs you will have access to it's rating:
#foreach($blogs as $blog)
{{ $blog->title }}
#if($blog->rating)
{{ $blog->rating->rating_total }}
{{ $blog->rating->rating_amount }}
#endif
#endforeach

Laravel blade regex image and output it?

Hi I have twitter api working to output details on the page however, this
field:
{{ $item['text'] }}
Gives me something like this:
RT #JacksSmokehouse: Don't forget #HappyHour tomorrow until 6pm - #loveJacks #live_oldham #OldhamHour #YouDontKnowJack https://twitter.com/shorturl, (with t.co)
So the image https:// I want to actually display it? is it possible to write in blade template a regex, something like this:
for each {{$item['text']}} take the url if there is any and output it below the text? can be a different variable.
Here's my full code:
<div>
#if(!empty($twitterItems))
#foreach($twitterItems as $key => $item)
{{ $item['text'] }}
#if(!empty($item['extended_entities']['media']))
#foreach($item['extended_entities']['media'] as $image)
<img src="{{ $image['media_url_https'] }}" style="width:100px;">
#endforeach
#endif
{{ $item['favorite_count'] }}
{{ $item['retweet_count'] }}
{{ $item['created_at'] }}
#endforeach
#else
There are no data.
#endif

How do you extract view code to a Component in Volt

I’m trying to extract some repeated view stuff into a component, in a similar way to a rails partial.
I want this:
{{ activity.each do |activity| }}
<div class="{{ activity.style_class }}>
{{ activity.text }}
</div>
{{ end }}
To become this:
{{ activity.each do |activity| }}
<:activity>
{{ end }}
But whenever I try to move my view code to app/activity/views/main/index.html
I get errors about activity not existing.
The trick is to pass in model when calling the component:
// app/main/views/main/index.html
{{ activity.each do |activity| }}
<:activity model="{{ activity }}">
{{ end }}
Then in your component you scope it onto itself like this:
// app/activity/views/main/index.html
<div class="{{ style_class }}>
{{ text }}
</div>

Laravel 4 adding numbers from foreach loop when count variable is supplied?

I am passing the array $cats to my laravel template view. It is a multidimensional array from a database transaction, containing category data. So it would contain data like:
$cat[0]['id'] = 1;
$cat[0]['name'] = 'First Category';
And so on. In my blade template I have the following code:
{{ $i=0 }}
#foreach($cats as $cat)
{{ $cat['name'] }}<br />
{{ $i++ }}
#endforeach
Which outputs:
0 First Category
1 Second Category
2 Third Category
Notice the numbers preceding the category name. Where are they coming from? Is this some clever Laravel trick? It seems that when you include a counter variable, they are automatically added. I can't find any mention of it anywhere, and I don't want them! How do I get rid of them?
Thanks.
You just need to use the plain php translation:
#foreach ($collection as $index => $element)
{{$index}} - {{$element['name']}}
#endforeach
EDIT:
Note the $index will start from 0, So it should be {{ $index+1 }}
The {{ }} syntax in blade essentially means echo. You are echoing out $i++ in each iteration of your loop. if you dont want this value to echo you should instead wrap in php tags. e.g.:
<?php $i=0 ?>
#foreach($cats as $cat)
{{ $cat['name'] }}<br />
<?php $i++ ?>
#endforeach
As an additional note, if you choose to work in arrays then thats your call but unless you have a specific reason to do so I would encourage you to work with object syntax, eloquent collection objects in laravel can be iterated over just like arrays but give you a whole lot of extra sugar once you get used to it.
#foreach($cats as $cat)
{{ (isset($i))?$i++:($i = 0) }} - {{$cat['name']}}
#endforeach
<? php $i = 0 ?>
#foreach ( $variable_name as $value )
{{ $ value }}<br />
< ? php $i++ ?>
#endforeach
if your $k is integer you can use {{ $k+1 }} or isn't integer you can use $loop->iteration
// for laravel version 4 and after
#foreach ($posts as $k => $post)
{{ $loop->iteration }}. {{ $post->name }}
#endforeach
You can actually use a built in helper for this: {{ $cat->incrementing }}.

How to use localization in Blade tag templates?

1st question:
I've inserted the localization in many types of texts and things, but I don't know how to import it into in the following forms:
{{ Form::label('name', 'here') }}
{{ Form::text('name', null, array('placeholder' => 'here')) }}
{{ Form::submit('here', array('class' => 'btn btn-success')) }}
{{ Form::button('here', array('class' => 'btn btn-default')) }}
I want it to be in the form label 'here' and in the placeholder of the text 'here'.
2nd question:
I am not allowed to insert it with links in my language file: text here blah blah BLAH?
Is there anyway to insert it with links?
Thanks in advance.
Supposing that your messages are stored in app/lang/en/message.php you can use the same way for all your cases:
In Blade template:
{{ Form::label('name', Lang::get('message.key')) }}
{{ Form::text('name', null, array('placeholder' => Lang::get('message.key'))) }}
{{ Form::submit(Lang::get('message.key'), array('class' => 'btn btn-success')) }}
In HTML tag mixed with some Blade expression:
BLAH
You can also use localization in Blade templates using strictly Blade syntax.
Given that you have a message in your /app/lang/en/messages.php that corresponds to the key "message_key", you can do :
#lang('messages.message_key')
to render the message in the locale that your application is configured to use.
So, The answers for both of your questions are:-
1) {{ Form::label('name', 'here') }}
Here, you need to change the "here" text hence laravel localization method can be used.For eg:-
{{ Form::label('name', '__("Here")' }} or
{{ Form::label('name', '__('message.here') }}.
2)< a href="{{ URL::to('text') }}">BLAH< /a>
Here, you need to change the label instead of link.
< a href="URL::to('text') ">{{__('message.BLAH')}}< /a>.
This is the simplest which works for me !
{!! Form::label('title', trans('users.addNewRecordsNameFieldLabel'),['class' => 'control-label']) !!}
I feel, already blade parsing is started the moment {!! or {{ is started
A possible answer to your second question:
You could set up a language file resources/lang/en/page.php like this:
return [
'sentence' => 'A sentence with a :link in the middle.',
'link_text' => 'link to a another page'
];
And use it in a Blade template like this:
{!! trans('page.sentence', [
'link' => '' . trans('page.link_text') . ''
]) !!}}
The result would be:
A sentence with a link to a another page in the middle.
You alse can use __() method
{{ __('app.name') }}

Resources