Yii framework custom headers - php

I want different headers for my landing page and all other pages in my webapp. What is the best way to do this?
AS far as my understanding goes, the header and footer are loaded from the view/layouts/main.php, In my case since I am using a theme, it is loaded from themes/new/views/layouts/main.php
Now I want the header section for all my pages except the landing page, to use the header section as defined in the "main.php", however the landing page should have a different header. How should I do this? Should I use a render partial? And since I am Yii newbie, how should I do this?
Can I also use one of the other layouts files, column1.php or column2.php? And how?
I am not looking for extensive hand holding here. I just need a heads up, as to how people have implemented similar functionality.

It partly depends on how different the headers are going to be in terms of your approach. If you want them to be completely separate, you could use additional layout files either in combination with main.php or instead of it.
You set the layout file at the start of the controller class with something like:
public $layout='//layouts/column2';
This will set the default layout for a controller. You can change the value in an action function or evaluate a condition in the "beforeAction" function.
The default generated admin pages (with "gii" or the command-line) utilize those column1.php and column2.php layouts with main.php and provide a decent example to see how they work. Just move the content out of main.php you want to customize and put it into separate layout files. If you still are sharing content, you can leave the shared content in main.php.
If it's just a matter of changing a few attributes, you can use $this->getAction()->getId() to get the action name and use that to change what content is loaded within the layout, e.g., a certain css or js file. Any complex logic you would want to do in the controller.
For something like a nav bar or similar, you could also use include or renderPartial base on an environment variable you set in the controller.

I'm usually create a widget like HeaderWidget with few view files and include in main layout. In controller or action define necessary view file of header and pass them into widget.
In base controller you can define property public $headerName = 'defaultHeaderView'
And set value depends of some conditions.
Of cource, you need to create BController extends CController and all other controllers extends from your BController

Related

Automatically Include wp-load on Admin Plugin View Pages

I am building a Wordpress plugin that allows admins to edit store hours from the backend of Wordpress. When a user clicks a department for which to edit the hours, a separate view ('hours.php') loads via jQuery
.load()
from this view, the list of hours for that department is supposed to be called up and displayed to the user. The functions that complete these tasks are in a separate class file. The problem is that in order for this class to be called I have to include the wp-load.php file as well as the class file. I do not want to do this as my hours.php file looks like this:
include('../../../../wp-load.php');
include('../class.libhours-database.php');
I know that is bad practice and do not intend on keeping it that way.
I read this article on query_vars and parse_request but I don't think this is exactly what I am looking for as I am not passing a URL at any point.
Remember: this plugin is ONLY accessible by admins and is only done on the backend.
Wordpress always loads wp-load.php, so you don't have to worry about that.
If your PHP script that handles yor Ajax call from the load() needs a separate class to create the HTML to return, then having it include the necessary PHP file that contains the class is perfectly appropriate.

how to echo head links in Zend controller which is set in bootstrap when layout is disabled

I am creating a document in one the pages in our website and I have to disable the layout.
All the CSS and JavaScript files are included via Bootstrap and I echo those links in layout.
So now I still need to add those links in controller of the page.
My code in bootstrap as follows:
$view->headLink()
->appendStylesheet('/css/bootstrap-cerulean.css')
->appendStylesheet('/css/bootstrap-responsive.css')
->appendStylesheet('/css/charisma-app.css');
I am trying to use this appended links in the related controller without having to type them one by one. Is There a way for me to pull this from Bootstrap and echo in controller action.
Thank you.
its not a good idea to do any view operations in controller action.
maybe think about using different layout in that action?

Passing Stylesheet to View from Controller

Is it possible to pass a Stylesheet to a view from a controller?
I have a master.layout file. This master layout is used for every page on the site. On the site the user can change colors and properties of their user profile, so on the profile pages I need to pass in either a custom user stylesheet, or custom style rules.
The only solution I have found is to include an if statement in the view and if the page is a profile page then check if $style is defined, if so include it. But this approach seems clunky.
Is it possible to define the layout and pass in scripts or style rules from within the controller itself?
Edit:
The style rules are stored in a database in columns such as profile.background_color and profile.heading_color. The controller then constructs them into either a stylesheet, and caches it, or includes the styles directly within a <style> tag on the profile view. I havn't decided which way to do it yet, but both are possible.
The advice about Facade was weird. That doens't really help you here.
You'll want to do a few things.
In your pages controller you can add:
$layout->with('extra_stylesheets', ['pages/someslug.css']);
Then that .css file should be processed as PHP using a little mod_rewrite and htaccess fun so it can be cached heavily (use more .htaccess for this, Google will help).
The layout then is the only location that needs to check for extra stylesheets and your pages view has a little less logic in it and your output HTML doesn't have a bunch of nasty <style> tags.
In master.layout, ensure you have a #section('userStyle')...#stop or similar.
In your controller, set the property protected $layout = 'master.layout';.
Build out the css as you are currently doing and store it in a php variable $css.
Then you can load up the css into your layout with $this->layout->userStyle = $css.

Yii Page Variation

I have a website that needs to have a variation of pages.
For example:
website.com/variation1/page1
website.com/variation2/page1
website.com/variation3/page1
All of which has the same DB and has the same function. The only difference is that these variations differs in page template ie. Header, Footer, Content, etc. This is for the purpose of analyzing page visits.
With that, what is the best way to do this? In Yii, there is a common layout used so header/footer depends on the layout. In this case, the header/footer should be specific to each page so the user can just modify the header/footer/content of a specific page without affecting other pages.
So, again what is the best approach for this?
Thank you in advance.
EDIT:
I think I know what I'm looking for here:
Is there a way to override/add from a layout the header in a view file?
Create Three different layouts say variation1.php, variation2.php, variation3.php, then based on the action(website.com/variation/pageone) or the parameter(website.com/variation/page/1) change the layout(in action).
I just found out a possible solution to approach page variations.
Under my view directory I created a directory varation which contains the pages i.e.
/view/variation/1/page1.php
/view/variation/2/page1.php
So when I want to render the varations I'll call each in the controller with parameters included.
Ex. $this->render('variation/1/page1') somewhat.
And in each page if you want to use a specific header info you may use
Register Meta Tag
Register Script File / Script
Register CSS File / CSS
Let me know if there are better ways of doing this.

Zend Framework - Last Code to Execute Before Layout is Rendered

I want to execute some code right before the layout is rendered, after all other code is executed. Where would I put that code?
I am specifically trying to modify the files referenced in the headLink, headScript, and inlineScript view helpers before they're used by the layout.
Here are the steps I want to take:
Loop over the files in those view helpers
Make a list of the local files
Remove local files from the view helpers
Reference the local file list as a parameter to a server script that combines them for a single HTTP request
Add that new combine script reference to the appropriate view helper
It doesn't appear that a Front Controller Plugin is going to help me accomplish this, and here's why:
postDispatch() gets executed after every controller action that's executed, and I need the full list of stylesheets/javascripts
dispatchLoopShutdown() gets executed after the controller action loop, but the layout has already been rendered at this point
As Rufinus suggested, I solved this by extending the View Helpers. My question from that angle as well as the solution is here.
see Orginal PDF created by Thorsten Ruf
(Mirror)
the very last part you can access via plugin should be dispatchLoopShutdown
EDIT:
For ZendFramework2 see http://zendframework2.de/en/cheat-sheet.html
or this gdoc

Categories