I creating blade templates by builder. I make .blade file based in json config. To don't do it in fly I'd like use cached templates.
In loop I fun functions which return me pure html. I get this html and set as #section('content') in some.blade.php
To make more complicated elements like carousel I use blade. I looks like
protected function getCarousel($element)
{
if(isset($element->componentData)){
$delay = (property_exists($element->componentData,'delay')?$element->componentData->delay:5000);
$navigationType = (property_exists($element->componentData,'navigationType')?$element->componentData->navigationType:'counter');
$size = (property_exists($element->componentData,'size')?$element->componentData->size:'standard');
$images = $element->componentData->images;
$data= ['images'=>$images,'size'=>$size,'navigationType'=>$navigationType,'delay'=>$delay,'attr'=>$this->getAttributesString($element->attr)];
return view('backend::components.carousel',$data)->render();
}
}
All works perfect until I need to only one section. But I need to 2 sections:
#content and #javascript
I'd like from my backend::components.carousel file get string like
#section('content')
html
#endsection
#section('javascript')
js code
#endsection
My environment
Laravel 5.4
PHP 5.6
is it possible or I have to don't use blade to make such code?
Thanks in advance for any hints.
Related
I am trying to add a new view to my laravel project, and it is just dumping the entire content on the page, including my blade code, instead of executing said code. For example, my page in the browser is this:
#extends('user.layouts.app') #section('title', 'New Reminder') #section('content')
New Reminder
#csrf
Title
Content
#endsection
When it should obviously be executing that code, not putting it on the page.
I have tried clearing the routes, and researched around but no dice.
I am calling this view the same way I am calling others in my code - in the controller like this:
return view('user.partials.stat_types.new_type');
Any help or advise is appreciated!
You must have the file name with the extension .blade.php if you want Blade to be used to compile the view. With just .php the PHP engine is used.
Change your view filename to have the .blade.php extension.
I'm trying to use the default bootstrap css (app.css) that ships with Laravel to style a section of my page - specifically, the form section of my registration page.
I don't want to include app.css in my html header as it gives me undesired effect on other parts of the page. So I want it to style only my html forms within the page.
Currently, I've used either the asset() or HTML::style() methods like this within my form section:
#section('form')
<style> #import "{{ asset('css/app.css') }}"; </style>
<form>...</form>
#endsection
OR
#section('form')
{{ HTML::style('css/app.css') }}
<form>...</form>
#endsection
Both method loads the style correctly, but affects the entire page instead of only the form elements.
I tried using the ViewComposer class to solve this problem by setting a variable in ViewComposer to my desired style - returning it only when I request the required view:
class ViewComposer
{
public function compose(View $view)
{
$data = [];
switch($view->getName())
{
...
case 'sections.register':
$this->data = ['style'=>"<style> #import \"". asset('css/app.css') . "\"; </style>"];
break;
}
return $view->with($this->data);
}
}
However, when I render the sections.register sub-view, I get the style variable like this:
#section('form')
{{ $style ?? '' }}
<form>...</form>
#endsection
the output on the browser is not parsed as css but displayed as-is:
<style> #import "{{ asset('css/app.css') }}"; </style>
So, is there a way I can parse external css for only a given view section within the html page and can it be achieved using the ViewComposer class?
UPDATE:
I was trying a few things and used this:
#section('form')
{!! $style ?? '' !!}
<form>...</form>
#endsection
The css is parsed but still applied to the entire page. I still need it applied to only the form section.
1. One option is to copy only the css you need and paste it into custom css and make a different layout for that view. But that can be tedious work as you said.
2. Another option is to prefix you app.css file. There is a software that can do that here is the tutorial. So if you prefix whole css file with for example: .laravel-app then you can wrap anything that you would like to be styled by app.css like this:
<div class="laravel-app">
<!-- Everything in here will be styled by app.css -->
</div>
This will help you in the long run with your project.
First of all, importing or loading css per-view will be bad for the performance of the application. So, using View Composer to load in css is not advisable. I took a cue from Denis Ćerić's answer, though it wasn't clear at first glance.
Also, the accepted answer on this post made things a little clearer.
The right way to achieve this is to use a css preprocessor. Popular ones are less and sass. I used sass because it is currently adopted by Laravel.
I installed sass on my windows machine following the instructions here.
Create a new scss file: app-custom.scss in the same folder as app.css.
Modify app-custom.scss using nested imports:
.app-form
{
#import 'app';
}
Generate app-custom.css using the sass command on Windows command line:
sass app-custom.scss app-custom.css
Change the class of your form to app-form:
#section('form')
<form class='app-form'>...</form>
#endsection
Include app-custom.css in your header using link tag:
<head>
<link href="{{ asset('css/app-custom.css') }}" rel="stylesheet">
</head>
and you are done.
HINT: if you want to use the style in app.css for multiple separate sections of your page, you can still achieve this from a single scss file. Just include the classes of each section in your scss file like this:
.section-1, .section-2, .section-3
{
#import 'app';
}
I am working with Laravel and created a master.blade view file to use on all my pages.
The master view yields mini-views inside.
On most mini-views everything works fine, but on some, I don't get the background image from the master.
The problem is I still get the nav-bar and the footer on those pages, which means they do recognize the master view.
What can be the reason for that?
On all pages I use the exact same way to include and to yield:
#extends('master')
#section('content')
#endsection
Thanks alot!
Just use full path to your file, start with /
Let's say I have a piece of jQuery javascript that binds to a div on load and dynamically defines an img in that div. Something like this:
$(document).ready(function() {
$("#container").html("<img href='/images/myimage.jpg/>');
});
If I were to use this inlined in a Laravel view, I'd be able to use HTML::image() and Blade templates to specify the location of image. Something like this:
$(document).ready(function() {
$("#container").html("{{ HTML::image('images/myimage.jpg', 'My image') }}");
});
If I were then to take that piece of javascript, and, instead of inlining it in the view, place it inside a separate .js file, say, public/js/image.js, I could have Laravel load it as an asset;
Asset::add('image.js', 'js/image.js');
However, since it's now treated only as an asset, neither the Laravel PHP nor the Blade templating code is processed, so we literally get the string {{ HTML::image('images/myimage.jpg', 'My image') }} in the resulting html, instead of the templated-in values.
Is there a good approach for something like this?
I'm doing this here. The way I found was:
1 - create a 'js' file inside the view directory tree, something like
app\views\javascript\mycustomJS.blade.php
2 - then render it wherever you need:
<script>
#include('javascript.mycustomJS')
</script>
It's blade, it will be processed as it should.
This is far from ideal, I know, but it works for me, for now. :)
I think you can put it in a php file. I've done this before with css.
Asset::add('image.php', 'js/image.php');
So I had a question on general organization of code for the Zend framework with regard to the layout.
My layout is basically this:
(LAYOUT.PHTML)
<div id='header'>
<?= $this->Layout()->header ?>
</div>
<div id='main'>
<?= $this->Layout()->main ?>
</div>
<div id='footer'>
<?= $this->Layout()->footer ?>
</div>
and so on and so forth. Now, in order to keep my code in my header separate from the code of my main and the code of my footer, I've created a folder for my view that holds header.phtml, main.phtml, footer.phtml. I then use this code to assign the content of header.phtml into $this->layout()->header:
(INDEX.PHTML)
$this->Layout()->header = file_get_contents('index/header.phtml');
$this->Layout()->main = file_get_contents('index/main.phtml');
$this->Layout()->footer = file_get_contents('index/footer.phtml');
That was working great, but I've hit a point where I don't want main to be static HTML anymore. I would like to be able to insert some values with PHP. So in my Controller in indexAction, I want to be able to load from my database and put values into index/main.phtml. Is there a way to do this without restructuring my site?
If not is there a way to do it so that I can have:
The ability to put code into different sections of my layout, such as Layout()->header, Layout->footer.
Separate these pieces into different files, so that they're easy to find and organize, like my index/footer.phtml, index/main.phtml etc.
Not have to put that code into quotes unnecessarily to turn it into a string to pass it to Layout()->header etc.
Thank you guys so much for your help.
-Ethan
Here is an idea:
Assign layout()->header the filename instead of the contents.
Put your code in this file
In your layout file, include() or require() the layout->header().
Since your layout headers/footers are now parsed, you can use them just like a view.
The ->header in $this->layout()->header is response segment. You can render parts of response using $this->_helper->viewRenderer->setResponseSegment('header'); in an action.
If you use
$this->layout()->header = $this->render('index/header.phtml');
It will even use the view, therefore keeping all your variables defined when rendering the header.
I would suggest using something like
<?php echo ($header = $this->layout()->header)?
$header : $this->render('headerDefault.phtml'); ?>
in your layout file - it will render a default header from the layout folder if the view script doesn't override it.
Have you tried looking at view helpers. They are a way of structuring view logic into reusable and modular code. In this case you would use a view helper to generate each of your required segments. So your example view script would look like
$this->Layout()->header = $this->header();
$this->Layout()->main = $this->main();
$this->Layout()->footer = $this->footer();
The benefit of using view helpers over include and require statements is that all of the file handling and name resolution is handled by the framework. The manual has more information on how to set up the paths and usage examples etc.
helpers are good. Another option is like the above, putting filenames in header/footer - put the template names and use $this->render($this->layout()->header)), etc etc. This is just like the include/require above, but more consistent.