I want to use a variable value into another one (both are in the same table as keys) and display in a blade layout the container variable value. Both variables are passed using the with method from the controller. Is that possible?
Here a quick demo of what I'm loooking for :
# controller method
public function build()
{
return $this
->view('emails.registration_following')
->with( ['name' => 'Nick', 'text' => 'Hello {{ $name }}'] ) # Here goes the thing
;
}
And the blade.php looks like :
<html>
<body>
<p> {{ $text }} </p>
</body>
</html>
The expect output by me is Hello Nickbut I got Hello {{Nick}}. I know that brackets here, are treated like String but how to circumvine that?
How about defining the variable in the function first:
# controller method
public function build()
{
$name = 'Nick';
return $this
->view('emails.registration_following')
->with( ['name' => $name, 'text' => "Hello $name"] ) # Here goes the thing
;
}
In this way, you have both $name and $text accessible in the view.
In fact, I did not need to pass two variables but just one and put in its value all needed other variables to form my expected text.
It was a bad idea to do what I was asking for. Thank you.
Have u tried like this below?
public function build()
{
$name = 'Nick';
return $this
->view('emails.registration_following')
->with( ['name' => $name , 'text' => 'Hello '.$name] ) # Here goes the thing
;
}
with( ['name' => 'Nick', 'text' => "Hello $name"] ) Please use double quote
Related
My view code is:
<?= $form->field($model, 'ReportSelection[]')->dropdownlist(['1' => 'Channel1', '2' => 'channel2','3'=>'channel3','4'=>'channel4'],['multiple'=>'multiple']); ?>
My controller code is:
return $this->redirect(['selectedreport','fromdate'=>$model->FromDate,'todate' => $model->ToDate,'reportselection' =>$model->ReportSelection,'reportoptions' => $model->ReportOption]);
My action function is:
public function actionSelectedreport($reportoptions,$reportselection,$fromdate,$todate)
I need $reportselection as argument here to use in:
foreach($reportselection as $i)
{
$selectlist = $selectlist . 'Channel'.$i.'Value,';
}
All action arguments are scalars by default. If you need accept array, you should explicitly specify this type in method signature:
public function actionSelectedreport($reportoptions, array $reportselection, $fromdate, $todate)
I'm using the Mail library in Laravel to send html email with custom data passed to a blade view.
The problem born when the mail has to render the html fetched from a row in the database which include a variable that i pass through the view.
This is my build function in my mailable class
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'url' => 'https://google.com',
'text' => $this->parameters->text,
]);
}
Then in the blade view:
<div>
{!! $text !!}
</div>
This is what the $text variable looks like:
<p>
<span>This is my text for the mail</span>
Click here to compile
</p>
The link href shoul contain the url variable value instead of not passing the variable name itself
A simple solution would be formating with php:
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'text' => str_replace('{{ $url }}','https://google.com',$this->parameters->text)
]);
}
I did not try by myself but you could make an attempt with Blade::compileString(), i.e.:
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'url' => 'https://google.com',
'text' => \Blade::compileString($this->parameters->text),
]);
}
I have two functions, the first one is working fine but the second one is not, in the second ones the variables appearing in the mail are {{$variableName}} unrendered.
I have checked as much other posts on SO as possible but no solution.
In the example below, in the first one the $userName correctly loads in the mail, whereas in the second one all variables show up as the variable name with the dollar sign.
I can confirm the $data variable is correct, and mails are even correctly sending using data from that variable.
public function workingMail($emailAddress,$userName) {
$message = '';
\Mail::send('workingtemplate', [
'userName' => $userName
], function ($message) use ($emailAddress) {
$message->from($this->fromAddress, $this->$this->fromAddressName);
$message->to($emailAddress);
$message->subject('Sample Subject');
});
}
public function notWorkingMail($data) {
$message = '';
foreach($data['namesAndAddress'] as $person) {
\Mail::send('notworkingtemplate', [
'name' => $person['name'],
'link' => $data['link'],
'fileName' => $data['fileName'],
'timeStamp' => $data['timeStamp'],
'action' => $data['action'],
'placeAddress' => $data['placeAddress']
], function ($message) use ($person,$data) {
$message->from($this->fromAddress, $this->fromAddressName);
$message->to($person['email']);
$message->subject($data['placeAddress'].' files have been updated.');
});
}
}
Thank you.
I had forgotten to add .blade to the name of the php file. It worked perfectly as soon as I did.
I want to have a Mustache template reference a partial where the partial adds data to the context as well. Instead of having to define the data in the data to the initial Mustache rendering.
I have a mockup in https://gist.github.com/lode/ecc27fe1ededc9b4a219
It boils down to:
<?php
// controller
$options = array(
'partials' => array(
'members_collection' => new members_collection
)
);
$mustache = new Mustache_Engine($options);
$template = '
<h1>The team</h1>
{{> members_collection}}
';
echo $mustache->render($template);
// viewmodel
class members_collection {
public $data;
public function __toString() {
$template = '
<ul>
{{# data}}
{{.}}
{{/ data}}
</ul>
';
$mustache = new Mustache_Engine();
return $mustache->render($template, $this);
}
public function __construct() {
$this->data = array(
'Foo Bar',
'Bar Baz',
'Baz Foo',
);
}
}
This gives an error like Cannot use object of type members_collection as array.
Is there a way to make this work? Or is using __toString not the right way? And would using a partials_loader or __invoke help? I got it working with neither but might miss something.
In your example above, members_collection isn't a partial, it's a subview. Two really small changes make it work: in the options array, change the partials key to helpers; and, in the parent template, change from a partial tag to an unescaped interpolation tag ({{> members_collection}} -> {{{members_collection}}}).
<?php
require '/Users/justin/Projects/php/mustache/mustache.php/vendor/autoload.php';
// controller
$options = array(
'helpers' => array(
'members_collection' => new members_collection
)
);
$mustache = new Mustache_Engine($options);
$template = '
<h1>The team</h1>
{{{members_collection}}}
';
echo $mustache->render($template);
// viewmodel
class members_collection {
public $data;
public function __toString() {
$template = '
<ul>
{{# data}}
{{.}}
{{/ data}}
</ul>
';
$mustache = new Mustache_Engine();
return $mustache->render($template, $this);
}
public function __construct() {
$this->data = array(
'Foo Bar',
'Bar Baz',
'Baz Foo',
);
}
}
I'm assuming you're using the bobthecow PHP implementation Mustache templating in PHP.
As of last time I checked, Mustache PHP it didn't support data driven partials. You want sort of a 'controller' backed a partial... however, currently partials just simple include-this-file style partials.
You're gonna have to build this yourself. Good luck!
As per here I've got the following controller:
class User extends CI_Controller {
public function Login()
{
//$data->RedirectUrl = $this->input->get_post('ReturnTo');
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('User_Login', $data);
}
//More...
}
and in my User_Login.php view file I do this:
<?php print_r($data);?>
which results in:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/User_Login.php
Line Number: 1
Do I need to load any specific modules/helpers to get the $data variable populated? If I print_r($this), I can see a lot of stuff but none of my data except in caches
Edit: To clarify, I know that calling the variable the same in the controller and view won't "share" it - it's out of scope but in the example I linked, it seems to imply a $data variable is created in the scope of the view. I simply happened to use the same name in the controller
Ah, the $data array's keys are converted into variables: try var_dump($title); for example.
EDIT: this is done using extract.
you should do it like :
echo $title ;
echo $heading;
echo $message;
Or you can use it like array.
In Controller:
...
$this->load->view('User_Login', array('data' => $data));
...
In View:
<?php print_r($data);?>
will show you the Array ( [title] => My Title [heading] => My Heading [message] => My Message )
you can pass a variable in the url to
function regresion($value) {
$data['value'] = $value;
$this -> load -> view('cms/template', $data);
}
In the view
<?php print_r($value);?>
You can't print the variable $data as it is an associative array....you may print every element of the associative array.....consider the following example.
Don't do as follows:
echo $data;
Do as follows:
echo $title;
echo $heading;
echo $message;
You can use this way also
$data['data]=array('title'=>'value');
$this->load->view('view.php',$data);
while sending data from controller to view we pass it in array and keys of that arrays are made into variables by code codeigniter and become accessible in view file.
In your code below all keys will become variables in User_Login.php
class User extends CI_Controller {
public function Login()
{
$data = array(
'title' => 'My Title', //In your view it will be $title
'heading' => 'My Heading', //$heading
'message' => 'My Message' //$message
);
$this->load->view('User_Login', $data);
}
}
and in your User_Login.php view you can access them like this:
echo $title;
echo $heading;
echo $message;