Put the content of a Blade section in a variable - laravel

In my blade template, there is a simple section :
#section('container')
My html markup here
#stop
that I call in a "master.blade.php" file :
#yield('container')
As I could understand, the yield function print the content of container. But I would like to know if I can put the content of container in a variable instead of ?
I would like to do something like that :
$myvar = #yield('container');
I know it is not correct syntaxically but there is the idea. I want to put the content of the section container in a variable.

When returning the view, read the html markup from a file and use with:
$html = File::get($filepath);
return view('yourview')->with(compact('html'));
Then in your Blade view file you can just output it like so:
#section('container')
{{ html }}
#stop
You can read more on in the Laravel Views Docs

Why don't you use some javascript to do something like this:
var somvar = $("#container").html();
or something like this:
<?php ob_start(); ?>
<div>Contents goes <b>here</b></div>
<?php $contents = ob_get_contents(); ?>

Does it have to be in the view? Can you do something like...
$myvar = View::make('yourview.blade.php')->with('somevars');

Related

What's good way to load header and footer view in codeigniter?

I'm using codeigniter 3x. I'm working on my website. I'm using include method in my view.
Like
<?php include('templates/header.php'); ?>
<h1>Home Page</h1>
<?php include('templates/footer.php'); ?>
Is this a good way to show header and footer in codeigniter.
Thank!
You are half your way, here is how you will be able to make it more dynamic, in your views file you should have a structure like this:
views
- header.php
- footer.php
- template.php
- home.page
In header.php you should have all your header and footer content which you wants to display on all pages.
Now in your template move all your includes.
template.php
<?php $this->load->view("header.php"); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view("footer.php"); ?>
here you notice $main_content variable, it is dynamic file name which we want to load in our controller. So lets assume you have a controller like this:
public function home()
{
$data['meta_title'] = $this->lang->line('home_meta_title');
$data['meta_description'] = $this->lang->line('home_meta_description');
$data['meta_keywords'] = $this->lang->line('home_meta_keywords');
$data['main_content'] = 'home';
$this->load->view('template',$data);
}
$data['main_content'] = 'home'; is loading your home.php file, you can also load from subdirectories like 'directory/home'. You can also pass any variable like I gave you above example with dynamic meta.

Change interpolation brackets - Angular2

I want to use Laravel Blade and AngularJS.
Is some way to change interpolate sintax, change {{text}} for [{text}] or somthing like that?
I already change all components.ts files adding the line:
interpolation: ["{[", "]}"],
but, where I write blade, app breaks...
Thanks a lot everybody ;)
You can define your Blade content tags in your routes.php file:
Blade::setContentTags('<%', '%>');
Blade::setEscapedContentTags('<%%', '%%>');
EDIT: You can add # in front of the brackets, so Blade won't render it.
#{ var }
or you can pass the directive for blade not to render part of the HTML using #verbatim keyword.
#verbatim
<div>
{{ var }}
</div>
#endverbatim

how to add a dynamic image src with laravels blade

My question is very simple. i use blade in laravel to show the images like
<img src="{{asset('assets/images/image.jpg')}}">
but what if my image is dynamic like:
<img src="{{asset('assets/images/$imgsrc')}}">
i cant use
<img src="{{asset('assets/images/{{ $imgsrc')}}">
because then i will end with:
<img src="http://localhost/assets/images/{{ $imgsrc">
How can i call to a $variable in a blade {{}} call?
How about
<img src="{{asset('assets/images/').'/'.$imgsrc}}">
EDIT
when you need printing more than one strings where there may be variables,functions... you need to concatenate them
Also asset('assets/images/') is equivalent to asset('assets/images') so you need to add / before printing your variable $imgsrc
Finally, in Blade {{"foo"}} is equivalent to <?php echo "foo" ?>
How can i call to a $variable in a blade {{}} call?
The normal PHP way. A blade {{}} is just the equilivant of <?php echo() ?>
So the answer to your question is:
<img src="{{ asset('assets/images/' . $imgsrc) }}">
Mine had multiple dynamic parts in the src and this is how I did it after getting in the right direction, thanks to #Laurence's answer.
<img src="{{asset('storage/'.$parentFolderName.'/'. $clientNameFolder .'/logo/').'/'.$event[0]->event_logo}}" class="event-page-country-logo">

How to grab content from section within blade template

I have written a training application with each page/slide of the training workbook as a seperate blade template file named as "page1.blade.php", "page2.blade.php" and so on. Each of these files has content of the kind:
#extends('en/frontend/layouts/training_modulename')
{{-- Page title --}}
#section('title')
Page Title
#parent
#stop
{{-- Page content --}}
#section('pageContent')
<div class="pageContentContainer">
<h2>Page Title</h2>
...
</div>
#stop
This works really well when being viewed page by page within the browser. However I also wish to automatically compile all pages into a PDF document. This is being done via dompdf which works amazingly well when I pass each pages html to it manually. However I wish to condense the #section('pageContent') section of each page into one large section which extends a different layout for passing to dompdf.
Given the above context my question is this:
Is there a method in Laravel's blade parser which would allow me to pass it a blade file and just get the rendered html from a particular section? The below pseudo-code demonstrates what I would like to be able to do.
$pages = array(...); // content of the directory
foreach ($pages as $page)
{
$renderedPage = Blade::render($page);
$title = $renderedPage->title;
$pageContent = $renderedPage->pageContent;
}
Instead of doing the normal return of view
return View::make('page');
You can instead store the view in a string
$view = View::make('page');
So then you can do your code something like this (not tested - but you get the idea):
$pages = array(...); // content of the directory
foreach ($pages as $page)
{
$renderedPage[] = view::make($page);
}

Zend Framework: View variable in layout script is always null

I set a view variable in someAction function like this:
$this->view->type = "some type";
When I access this variable inside layout script like this:
<?php echo $this->type ?>
it prints nothing. What's wrong?
My application.ini settings related to layout
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = "layout" ; changed 'default' to 'layout'
Edit
This thread suggests the alternate solution, but looking for solution to above problem. And this was working in Zend 1.6.2. I just upgraded to 1.10 and it stopped working.
Edit
If I set this view var inside any _init Bootstrap function, it works.
If you want to assign something to your layout you have to go an other way:
// get the layout instance
$layout = Zend_Layout::getMvcInstance();
// assign fooBar as Name to the layout
$layout->name = 'fooBar';
I believe the layout view object and the action view object are separate instances of the Zend_View class.
I think this is the correct way to pass variables from the controller to the layout:
/**
* Controller action
*/
public function indexAction()
{
$this->_helper->layout()->assign('myName', 'John Doe');
}
and then in your layout script you can access the variables by referencing the layout object like this:
<html>
<body>
<?php echo $this->layout()->myName; ?>
</body>
</html>
Do you have the following entry in your application.ini file?
resources.view[] =
So, you can initialize the view with no options and use it through:
<?php echo $this->type ?>

Resources