Parse content from function as were it Blade contents - laravel

I've got a blade view in which I load another string via a function called retrieveSomeContent()
retrieveSomeContent() returns a string, which could hold 'references' like {{ $action->url }}. The data itself is available in the view.
// myView.blade.php
// simplified
...
{{-- doesn't work
displays {{ $action->url }}
--}}
{!! retrieveSomeContent('section1B') !!}
{{-- works --}}
{{ $action->url }}
...
Off course the view right now will just displays {{ ... }} as text instead of parsing it to http://......
How would I be able to parse the string generated via retrieveContent()?

Related

Passing variables to email footer in Laravel

I would like to show a disclaimer in the footer saying
This email was sent to {recipients email address}.
but i can't figure out how i can get the email address into the standard footer blade.
Here is what I have:
In my notification component I make the following call with $rcp_email having the email address of the recipient:
return (new MailMessage)
->subject('Contact Form Submitted')
->markdown('emails.generic.contactform', [
'first_name' => $this->first_name,
'message' => $this->message,
'rcp_email' => $this->rcp_email,
'url_security_issue' => $url_security_issue,
]);
The emails.generic.contactform looks like this
#extends('emails.main')
#section('content')
(body of email)
#endsection
emails.main looks like this
#component('mail::message')
#yield('content')
#if (isset($unsubscribe_url))
(html code for unsubscribe)
#endif
#endcomponent
the standard vendor\mail\html\message blade looks like this:
#component('mail::layout')
{{-- Header --}}
#slot('header')
#component('mail::header', ['url' => config('app.front_end_url'), 'logo' => asset('logo-image-white-circular-background.png')])
{{ config('app.name') }}
#endcomponent
#endslot
{{-- Body --}}
{{ $slot }}
{{-- Subcopy --}}
#isset($subcopy)
#slot('subcopy')
#component('mail::subcopy')
{{ $subcopy }}
#endcomponent
#endslot
#endisset
{{-- Footer --}}
#slot('footer')
#component('mail::footer')
© {{ date('Y') }} {{ config('app.name') }}. #lang('All rights reserved.')
#endcomponent
#endslot
#endcomponent
I traced each level of blade and $rcp_email is first of all not availble in the message blade. I tried to pass it on by changing the first line of emails.main to
#component('mail::message', ['rcp_email' => $rcp_email])
but in either case when i try to use the variable in the message blade, i get the error Undefined variable: rcp_email
In an ideal case, I would want to add the html code and make use of the variable in the actual footer blade, but I don't know if that's possible.
I would much appreciate if there is any hints/any docus or articles you can share that describes in depth how this works?
Thanks,
Goppi
hopefully this helps for others with issue
From what I did, when you use #component('mail::message', ['rcp_email' => $rcp_email]) it will pass the variable $rcp_email to the message blade.
Here change your message blade #component('mail::footer') to #component('mail::footer', ['rcp_email' => $rcp_email]) to pass it again to your footer blade.
In footer blade access the variable like {{$rcp_email}}
so example would be <p>sent to {{$rcp_email}}</p>
Also as a note, when passing the variable like ['name' => $variable] it uses the name as the variable in the next blade. so here you would access it like $name
Thanks #Jack C - your solution didn't work for me back then as you can see from my original post. I tried to pass rcp_email on to the message blade using
#component('mail::message', ['rcp_email' => $rcp_email])
and when I tested $rcp_email within the message blade, it came back with undefined variable. That said, I didn't figure out why it wasn't working, but I used #component with variables for other blades without issues. Perhaps it didn't work due to caching issue.
The solution that I came up with back then opened itself up once I decided not to use the standard email template, but design my own.
The emails.generic.contactform changed as follows:
#extends('emails.layout.layout')
#section('content')
{{-- body of email --}}
#endsection
The emails.layout.layout blade looks like this:
#component('mail::layout')
{{-- Content --}}
{{-- Footer --}}
#slot('footer')
#component('mail::footer')
{{ $recipient ?? '' }}
#endcomponent
#endslot
to summarize, my understanding on how to pass variables between blades is as follows:
extend - extends allows the layout blade to use all variables with the same names as the extended blade.
#extends('layout')
components have no access to the variables of the parent blade. Instead they need to be passed on. There are two ways to pass them on: as one or more variables or as a slot. In the example below $name is available within the component footer.
#component('footer', ['name' => $rcp_email])
#endcomponent
component - the second way is as a slot. In the example below the variable $slot would contain the email address.
#component('footer')
{{ $rcp_email }}
#endcomponent

how to escape curly braces in vue.js

I have data in my database that may contains curly braces{{ }}.
{{-- inside app.blade.php --}}
<!-- vue app -->
<div id="app">
...code
<div> {{ $data }} </div>
...code
</div>
so if I want to display it to the user this data cause problem if it's inside Vue app. and vue think it's javascript codes to execute.
for example if the $data is equal to {{ title->links() }} then I get an error and the whole app doesn't compile at all. (it passes the blade template).
[Vue warn]: Error compiling template:
invalid expression: expected expression, got '>' in
_s(title->links())
Raw expression: {{ title->links() }}
305| <div>{{ title->links() }}</div>
| ^^^^^^^^^^^^^^^^^^^^^^^
what is the best way to escape {{ }} curly braces for user data (in Vue.js)??
You need use the v-pre or v-html directive:
<div v-pre>{{ data }}</div>
or
<div v-html="'{{ data }}'"></div>
ref link https://v2.vuejs.org/v2/api/#v-pre

Not allowing code to excute

In Laravel 5, using {{ code }} will escape code and show it as text, but if the code is already encoded say HTML, and is displayed inside the {{ }} it will execute, is there a way for this not to happen?
Use {!! $your_code !!} instead of {{ }}
If you do not want your data to be escaped, you may use the following syntax: {!! $text !!} to display html
https://laravel.com/docs/5.5/blade#displaying-data

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

Using multidimensional array input names with Laravel 4.2 Form facade throws error on postback

I'm having an issue in Laravel 4.2 with the Form facade when using input names that represent multidimensional arrays. The form will load, display, and post data correctly unless there are values set in Input::old() (because of failed validation, etc.).
Here is a simple example that shows the problem:
routes.php:
Route::get('input-test', function() {
return View::make('input_test.index');
});
Route::post('input-test', function() {
return Redirect::back()->withInput(Input::all());
});
input_test/index.blade.php:
<!doctype html>
<html>
<head>
<title>Input Array Test</title>
</head>
<body>
{{ Form::open(array('url' => 'input-test')) }}
{{ Form::label('customer[some_customer_field][]', 'Customer Field:') }} <br>
{{ Form::text('customer[some_customer_field][]', null) }}
{{ Form::submit('Submit') }}
{{ Form::close() }}
</body>
</html>
Submitting this form will throw an error:
htmlentities() expects parameter 1 to be string, array given
Is there a way to get inputs with these types of names to work on postback?
That way is not the proper one.
Following one is it
{{ Form::label('customer[0][field_1], 'Customer Field:') }} <br>
{{ Form::text('customer[0][field_2]', null) }}
After that, if you want to duplicate it, you must use
{{ Form::label('customer[1][field_1], 'Customer Field:') }} <br>
{{ Form::text('customer[1][field_2]', null) }}
But if you want just get a simple array, you must use
{{ Form::label('customer[field_1], 'Customer Field:') }} <br>
{{ Form::text('customer[field_2]', null) }}

Resources