In Laravel I am trying to append one of my header sections from an include in the footer - What am I doing wrong? - laravel

Simplified my main.blade.php template looks like this...
<html>
<head>
#inlcude("header")
#yield("addToHeader")
</head>
<body>
#yield("content")
#include("footer")
</body>
</html>
Let's say my controller points to login.blade.php and it provides addToHeader and content sections. When my footer is included, I'd like to append the addToHeader section but I just can't get it to work.
In my footer I have tried
#section("addToHeader")
#parent
This is the added content
#yield("addToHeader")
#stop
And variations of this.
Now, for the sake of doing it right and in hopes that I might not spend my resources on doing this the incorrect or inefficient way, here is what I am trying to accomplish by doing this...
When a user logs in every time they try to load a new page, I want the website to throw a message to the user (in my case I am actually "locking" the page down with a modal) that lets the user know he/she must finish setting up her profile before anything else can be done. Is there a controller or route that doesn't care which page/action is being requested and allows me to send extra data to the template - even if it is just $data["lockdown"]=true?

I don't know if it's the best way to do it but i would go for something like this.
Using a session variable to know if the user has to set-up his profile or not:
Session::put('profile_uncompleted',true);
You can set this variable when the user log-in for exemple.
Then on your main.blade.php you can check if you have to display your modal:
#if(Session::has('profile_uncompleted')
#include('modal')
#endif
Where modal contains your Html modal content.

Related

Why isn't my view redirecting to a controller?

I have a html form inside one of my views that is supposed to get an image the user uploads and save it, the form is below:
<form class="col-lg-12" action="/banner/store/">
The form itself is quite large, but later on there's a to end it. It was supposed to redirect me to the banner/store where my Banner controller have a dd() function so I can confirm everything is okay. But instead, I get a blank white screen and the url goes banner/store/?.
The routes to the BannerController seems to be correct since banner/create works, the problem is when I go from create to store, but anyways, here's how I set the Routes:
Route::resource('banner', 'BannerController');
Any ideas why my banner/store won't work? And why is there a question mark on the end of the url? Sorry if this may be a dumb error, I'm still learning coding.
Your action is is wrong. When you use the resource static method then the store-url is the same as the GET url.
You can achive your goal with:
<form class="col-lg-12" action="{{ route('banner.store') }}" method="POST">
See more information in documentation.
When you updating your banner, you can't use the browser native form with PUT.
See in this document how laravel will handle that for you.

laravel blade template - how to remove a part from extended page

Note: Like i said in the comment, i am working relatively large project. I can't change existing codes. Just try to add some code blocks for one page.
I have a template blade. It has meta yield. But also included one meta.blade.php, that contains all meta tags. But i don't want to include metapage for some of my pages. There is the template for visualization:
my_template
<header>
#yield('meta')
#include('metapage')
#yield('style')
#yield('js')
</header>
my view.blade.php
#extends('my_template')
#section('meta')
<meta description...>
#endsection
#section('style')
//content
#endsection
#section('js')
//content
#endsection
My question is: Is there a way to make something like this:
#extends('my_template')->except('metapage')
I know there isn't exist something like that. But i need that. I hope somebody can give me a solution.
There is a certain solution which I do:
First one is to create multiple template and extend them as your requirements.
Second one is to disable sub parts.
Third one to create parent template having little things, then create child template which is extending parent and do extra things here. use it as your need.
If You are working on existing project and you have a lot of pages then First & third one is a better solution for it because you could make changes only in front end without affecting class code.
You could make the meta a component rather than an include, then your template would look the same and your view would be e.g.
#extends('my_template')
#section('meta')
#component('meta')
#slot('description', 'my amazing description')
#endcomponent
#endsection
// other code here as usual
Your component is then responsible for checking what exists and what doesn't, e.g. like this:
#isset($description)
<meta name="description" content="{{ $description }}">
#endisset
#isset($title)
<title>{{ $title }}</title>
#endisset
// etc, the title is just an example
Documentation: https://laravel.com/docs/5.6/blade#components-and-slots
Take the Tag Name Which You Want To ignore And Apply the CSS
ex: If I need to Remove the Footer From the Extended Page make
footer {
display: none;
}

Using include in different situations

I have a boilerplate that has various includes, one being:
#include('nav')
I would like to use the same boilerplate for the CMS of my site, but use a different nav.
What would be the best way of getting the boilerplate to include a different nav when the user is using the CMS:
#include('nav-cms')
This is more of an architectural question, and the answer is that you can probably do this many, many ways. However, the answers can be Laravel specific, so here goes:
One method would be to change the include statement to be:
#include($navView)
And then either in your controller, or using view composer, you should set that variable appropriately.
Alternatively, you can do it using sections:
// layout.blade.php
<html>
<head></head>
<body>
#section('nav')
#include('nav')
#show
#yield('content')
</body>
</html>
// some-frontend-view.blade.php
#extends('layout')
#section('content')
Content here
#stop
// some-cms-view.blade.php
#extends('layout')
#section('nav')
#include('nav-cms')
#overwrite
#section('content')
CMS content here
#stop
That way it assumes frontend nav, and then you override it in the CMS for the CMS nav. Alternatively, instead of defaulting to 'nav' in the layout, you could use #yield, and specify it in the some-frontend-view.blade.php file as in the some-cms-view.blade.php file.

Dynamic display in Grails

I have this problem I'm facing. I have been working on a project using Grails based on the advice from a friend. I'm still a novice in using Grails, so any down to earth explanation would be highly welcomed.
My project is a web application which scans broken or dead links and displays them on a screen. The main application is written in Java, and it displays the output (good links, bad links, pages scanned) continuously on the system console as the scan goes on. I've finished implementing my UI, controllers, views, database using Grails. Now, I will like to display actively in a section of my GSP page say forager.gsp the current link being scanned, the current number of bad links found and the current page being scanned.
The attempts I have tried in implementing this active display include storing the output my application displays on the console in a table in my database. This table has a single row which is constantly updated as the current paged scanned changes, number of good links found changes and number of bad links found changes. As this particular table is being updated constantly, I've written an action in my controller which reads this single line and renders the result to my UI. The problem I'm now facing is that I need a way of constantly updating the result being displayed after an interval of time in my UI. I want the final output to look like
scanning: This page, Bad links: 8, good links: 200
So basically here is my controller action which reads the table from the database
import groovy.sql.Sql
class PHPController {
def index() {}
def dataSource
def ajax = {
def sql = new Sql(dataSource)
def errors = sql.rows("SELECT *from links")
render (view: 'index', template:'test', model:[errors:errors])
}
}
Here is the template I render test.gsp
<table border="0">
<g:each in="${ errors }" var="error">
<tr><td>${ error.address }</td><td>${ error.error}</td><td>${ error.pageLink}</td></tr>
</g:each>
</table>
For now I'm working with a test UI, which means this is not my UI but one I use for testing purposes, say index.gsp
<html>
<body>
<div><p>Pleaseeee, update only the ones below</p></div>
<script type="text/javascript">
function ClickMe(){
setInterval('document.getElementById("auto").click()',5000);
alert("Function works");
}
</script>
<div id="dont't touch">
<g:formRemote url="[controller:'PHP', action:'ajax']" update="ajaxDiv"
asynchronous="true" name="Form" onComplete="ClickMe()" after="ClickMe()">
<div>
<input id="auto" type="button" value="Click" />
</div>
</g:formRemote>
<div id="ajaxDiv">
<g:render template="/PHP/test"/>
</div>
</body>
</html>
The div I'm trying to update is "ajaxDiv". Anyone trying to answer this question can just assume that I dont have an index.gsp and can propose a solution from scratch. This is the first time I'm using Grails in my life so far, and also the first time I'm ever dealing with ajax in any form. The aim is to dynamically fetch data from my database and display the result. Or if someone knows how to directly mirror output from the system console unto the UI, that will also be great.
It sounds like a form would be appropriate for your needs. Check out the Grails documentation on forms. You should be able to render a form with the values you would like without too much trouble. Be sure to pay attention to your mapping and let me know if you have any questions after you have set index.gsp up to render a form for your values.

How to modify body class in MVC3 view page

Just wanted to add a class="myClass" in body tag. Is there any html helper or something else can do this in MVC3 view page? Please advise, thanks.
This is very similar to Aaron's solution, but doesn't have the weight of a section (which at least in my mind, are for larger blocks of content than a single string). The simplest way is to pass a variable with the ViewBag.
In your layout, just print out the class for the body tag, plus any other page specific variables (page title, extra css/js scripts, etc...)
_Layout.cshtml:
<html>
<title>#ViewBag.Title</title>#* Can easily add in per page titles too *#
<body class="#ViewBag.BodyClass">
#RenderBody()
</body>
</html>
Then, variables set in your view get passed upwards to the layout:
Index.cshtml:
#model MyViewModel
#{
ViewBag.Title = "This page title!";
ViewBag.BodyClass = "...";
}
While you may have full control of the HTML a solution was what was needed so here is one ;-)
In the _layout.cshtml page
<body class="#RenderSection("BodyClass", false)">
This will look for a section in all the child pages but says don't worry if it can't find one
Then in your child views just do this
#section BodyClass {productList}
Keep it on one line and then the outputted HTML will look fine, also you can then build up the class names as well.
#section BodyClass {productList generic}
This idea goes perfect with DOM-Ready page specific code, why not checkout
http://paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/
Or my extended version here
https://github.com/AaronLayton/H5BP-Core
My way lets you do page specific code, but allows you to keep all of the Javascript in separate pages so each page becomes easily manageable. The final step would be to concatenate and minify all the JS into 1 file ;-)

Resources