Get Nunjucks templates content without render - nunjucks

I need get Nunjucks templates raw content without render it.
For example:
{% block css %}
<link rel="stylesheet" href="style1.css" />
<link rel="stylesheet" href="style2.css" />
{% endblock %}
How can i get below result?
<link rel="stylesheet" href="style1.css" />
<link rel="stylesheet" href="style2.css" />

Related

My css files don't run in my single blog page

I use laravel 8, I'm creating a blog. The problem is when i click one of my posts for seeing the single blog page, the page don't come due to problem to see all the css.
'''
<!-- Bootstrap Css -->
<link rel="stylesheet" href="{{url('css/bootstrap.min.css')}}">
<!-- Font Awesome CSS -->
<link rel="stylesheet" href="{{url('css/font-awesome.min.css')}}">
<!-- Slick Nav CSS -->
<link rel="stylesheet" href="{{url('css/slicknav.min.css')}}">
<!-- Cube Portfolio CSS -->
<link rel="stylesheet" href="{{url('css/cubeportfolio.min.css')}}">
<!-- Magnific Popup CSS -->
<link rel="stylesheet" href="{{url('css/magnific-popup.min.css')}}">
<!-- Fancy Box CSS -->
<link rel="stylesheet" href="{{url('css/jquery.fancybox.min.css')}}">
<!-- Nice Select CSS -->
<link rel="stylesheet" href="{{url('css/niceselect.css')}}">
<!-- Owl Carousel CSS -->
<link rel="stylesheet" href="{{url('css/owl.theme.default.css')}}">
<link rel="stylesheet" href="{{url('css/owl.carousel.min.css')}}">
<!-- Slick Slider CSS -->
<link rel="stylesheet" href="{{url('css/slickslider.min.css')}}">
<!-- Animate CSS -->
<link rel="stylesheet" href="{{url('css/animate.min.css')}}">
<!-- Radix StyleShet CSS -->
<link rel="stylesheet" href="{{url('css/reset.css')}}">
<link rel="stylesheet" href="{{url('style.css')}}">
<link rel="stylesheet" href="{{url('css/responsive.css')}}">
<!-- Radix Color CSS -->
<link rel="stylesheet" href="{{url('css/color/color1.css')}}">
<link rel="stylesheet" href="#" id="colors">
'''
[What my single blog page looks like ][1]
if i don't use the stylesheets 'reset.css' it looks like :
single blog page without stylesheet reset.css
You can use asset helper instead of url
<link rel="stylesheet" href="{{asset('css/reset.css')}}">

All of a sudden external links not working in barryvdh/laravel-dompdf

I am using barryvdh/laravel-dompdf for quite long.
Using external CSS (From local) and font (From CDN), It was working fine and suddenly the design collapsed, also the font not working so I've checked in the view and came to know the view is not loading the CSS & fonts.
I am using Laravel 5.5 and barryvdh/laravel-dompdf version is 0.8.6.
Tried in Laravel 8 with barryvdh/laravel-dompdf version 0.9.0 also not working.
Code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Receipt | {{ Auth::user()->name }} - {{ $setting->name }}</title>
<link href="{{ asset('css/bootstrap.css') }}" rel="stylesheet" type="text/css" media="all" />
<link href="{{ asset('css/main.css') }}" rel="stylesheet" type="text/css" media="all" />
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700" rel="stylesheet" type="text/css" media="all" />
<style type="text/css">
/* Another font (Chinese) loaded from local folder for particular tags */
#font-face {
font-family: SimHei;
src: url('{{asset('fonts/simhei.ttf')}}') format('truetype');
}
body{
font-family: Open Sans, Sans-serif; line-height: 1;
}
p {
font-size: 18px;
margin-bottom: 0 !important;
}
</style>
</head>
For CSS, I have tried to update
<link href="{{ asset('css/main.css') }}" rel="stylesheet" type="text/css" media="all" />
to
On Local:
<link href="{{ public_path('css/main.css') }}" rel="stylesheet" type="text/css" media="all" />
<link href="{{ base_path().'/css/maincss' }}" rel="stylesheet" type="text/css" media="all" />
<link href="http://127.0.0.1:8000/css/main.css' }}" rel="stylesheet" type="text/css" media="all" />
On server:
<link href="var/www/html/project/public/css/main.css" rel="stylesheet" type="text/css" media="all" />
For the font, I have tried to have it in a local folder as a .ttf file, and importing in style still doesn't work.
I've managed to solve this issue.
Posting here incase it can help someone.
Replaced barryvdh/laravel-dompdf with barryvdh/laravel-snappy
Package Link : https://github.com/barryvdh/laravel-snappy
My external styles, images & fonts are not loaded.
For styles i have created a blade file, updated it with css codes and included it in the pdf template blade.
For images i have created an absolute path
<img src="{{ public_path('images/img1.jpg')}}" alt="No Image" />
For font i have installed the font on the server,
I am using ubuntu 16.04, I have installed open sans and SimHei (Chinese font) and updated the font-family to open sans & simhei in my application.

Conditional loading of mix css in blade header blade template

I would like to define header.blade.php which then will be included in:
index.blade.php
dashboard.blade.php
basic.blade.php
advanced.blade.php
and I would like to load different mix CSS styles depending on which blade view is this header.blade.php view included in currently I am loading all the styles:
<link rel="stylesheet" href="{{ mix('/css/all.css') }}" />
<link rel="stylesheet" href="{{ mix('/css/dashboard.css') }}" />
<link rel="stylesheet" href="{{ mix('/css/basic.css') }}" />
<link rel="stylesheet" href="{{ mix('/css/advanced.css') }}" />
But I would like to add condition "if included from dashboard.blade.php load only <link rel="stylesheet" href="{{ mix('/css/all.css') }}" /> and <link rel="stylesheet" href="{{ mix('/css/dashboard.css') }}" />
",...
Is that something that can be accomplished in blade views in Laravel 5?
I think you need to use the #yield directive. It allows pages that include a layout to define their own different implementations of the same section.
https://laravel.io/forum/09-02-2014-using-section-and-yield

Js and bootstrap are not working when the file is in folder. Laravel

When i am including the file head.blade.php in views.home, JS and Bootstrap are working properly. But when i am trying to include head.blade.php in views.posts.create JS and bootstrap are not working. I found in previous questions that i shoult include them like that:
<link rel="stylesheet" href="rel="stylesheet" {{URL::asset('css/responsive.css')}}"> but didn't helped.
This are my routes:
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/font-awesome.min.css" rel="stylesheet">
<link href="css/animate.min.css" rel="stylesheet">
<link href="css/prettyPhoto.css" rel="stylesheet">
<link href="css/main.css" rel="stylesheet">
<link href="css/responsive.css" rel="stylesheet">
how it is
How should be
Try adding a / to the front of your url:
<link href="/css/bootstrap.min.css" rel="stylesheet">
<link href="/css/font-awesome.min.css" rel="stylesheet">
<link href="/css/animate.min.css" rel="stylesheet">
<link href="/css/prettyPhoto.css" rel="stylesheet">
<link href="/css/main.css" rel="stylesheet">
<link href="/css/responsive.css" rel="stylesheet">
Note: folder path should be "project/public/css/"
This can also be done with the url() helper function:
<link href="{{ url('/css/responsive.css') }}" rel="stylesheet">
Take a look at the Laravel URL helper functions documentation for more information.

Blade passing values from view to master and then pass it to header.blade

I have a situation where i have a view file users.blade.php which extends master. and the master includes another file called header.blade.php, i want to pass some data from users.blade to header.blade.
Here is the simplified version of the files
/****** Users.blade.php *********/
#extends('shared.master')
#section('title', 'Dashboard')
#section('pagecss')
<link rel="stylesheet" href="links to css file" />
#endsection
Here is the master.blade.php
/******Shared/master.blade.php ********/
<html>
<head>
<title>#yield('title')</title>
#include('shared.header')
</head>
<body >
#yield('content')
</body>
</html>
Here is the header file
/******Shared/header.blade.php ********/
<link rel="stylesheet" href="links to bootstrap" />
#yield('pagecss')
<link rel="stylesheet" href="links to other files" />
#yield('pagecss') doesn't work in the header.blade, but it does work in master.blade. Now i cant paste it in master.blade because there are some files which override other files, so it has to be in a specific order. Any ideas how to make the yield work in header.blade?
EDIT:
I do like Bharat Geleda approach, i have modified the code to include some heredocs to make it more convenient. But i think its kind of a hack and still looking for a better solution.
/********** Users.blade.php ***********/
#extends('shared.master')
#section('title', 'Dashboard')
#section('pagecss')
<link rel="stylesheet" href="links to css file" />
#endsection
<?php $pagecss = <<<CSS_FILES
<link rel="stylesheet" href="links to page css" />
CSS_FILES;
?>
/*********** Shared/header.blade.php **********/
#if(isset($pagecss))
{{-- */ echo $pagecss;/* --}}
#endif
A possible solution would be to do this
<?php $pagecss = '<link rel="stylesheet" href="links to css file" />'?>
/****** Users.blade.php *********/
#extends('shared.master')
#section('title', 'Dashboard')
master.blade.php would remain the same
/******Shared/master.blade.php ********/
<html>
<head>
<title>#yield('title')</title>
#include('shared.header')
</head>
<body >
#yield('content')
</body>
</html>
And then in your header file you could do this
/******Shared/header.blade.php ********/
<link rel="stylesheet" href="links to bootstrap" />
#if(isset($pagecss))
{{ $pagecss }}
#endif
<link rel="stylesheet" href="links to other files" />
EDIT: TESTING OUT QUESTION
main.blade.php (master in your case)
<html>
<head>
#include('test.header')
</head>
<body >
#yield('content')
</body>
</html>
header.blade.php
<link rel="stylesheet" href="links to bootstrap" />
#yield('pagecss')
<link rel="stylesheet" href="links to other files" />
users.blade.php
#extends('test.main')
#section('content')
CONTENT
#endsection
#section('pagecss')
<link rel="stylesheet" href="links to css file" />
#endsection
OUTPUT :
<html>
<head>
<link rel="stylesheet" href="links to bootstrap" />
<link rel="stylesheet" href="links to css file" />
<link rel="stylesheet" href="links to other files" />
</head>
<body >
CONTENT
</body>
</html>
I guess this is as expected.
Simple if it's working on homepage and not in inner views, that means you've used it, and Blade will take only the higher level and resolve it (include its content where it should be placed within #yield).
What I'm trying to say that if you got a #yield within your header file and you've used it as #section('pagecss') later you got another view as in your case Users.blade.php with the same statement #section('pagecss') blade won't take the inner one.
Solution
Nice way but foolish:
create another #yield within your shared header blade file and name it as level two or like so. i.e #yield('pagecss-2')
Good way:
using #parent, this directive is going to append (rather than overwriting) content to the layout
as mentioned here within docs of laravel

Resources