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.
Related
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?
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.
I'm quite new to Drupal and want do some editing of the header. I want a custom toolbar to appear on every page. I've already created that toolbar in a file called toolbar.php. It has a layer which is fixed and will appear on top of every page.
How do I include the toolbar.php in the header template of drupal?
The toolbar refers to $user which is a global Drupal variable and I want to test toolbar.php before publishing it to the site. Is there anyway I can do that?
Regards,
Dasith
Of the two methods above the first is easier if you understand the basic idea of html and CMS templates, the second will be easier if you are a programmer.
First thing to check is that you really need to do this! Can't you restyle one of the existing menus (Primary or secondary) to do this - will make your life (and anyone who works on the site in the future) a lot easier.
The other thing you can do is look into adding an output region, basically something where you put the php into a drupal friendly format and then effectively do a 'drupal print'. This is how the toolbar, search box etc are done. You still need to alter the templates as above.
Yes for sure. If you want to have the html produced by your function/file appear on every page of the site then you will need to over-ride the page.tpl.php file in the theme you are using and essentually add the html to that file.
To gain access to the $user variable, just declare it in your script.
global $user;
open page.tpl.php file in a code editor and save as page-front.tpl.php (with two dashes if you are using Drupal 7.. one dash with Drupal 6) and upload it to your theme's directory. Clear your cache by going to configuration->Performance->Clear All Cache. Then referesh the page. Now your homepage is using page-front.tpl.php as it's template file. Every page will need its own template file. The page machine name comes after the hyphen so the user page template uses page-user.tpl.php. You can edit it as you want. The proper way to really do this is to use hook_theme() to pass variables to the template file. One variable could be the html which creates your custom header.
See also http://drupal.org/node/457740 Beginners Guide to over riding theme output
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
How do I with Silverstrip just create a plain HTML page ?
I am trying to add a page, then edit HTML to put in my own script tags etc, and won't let me.
i think you're talking about editing the page templates (mysite/templates/Page.ss), right? a common mistake is not flushing the template cache whenever you change a template. just add ?flush=1 to your page url to do so.
also see the docs on URL Variable Tools for further information on this.
additionally, in case you don't want to have pages in the cms, you might be interested in this tutorial on ssbits to bypass the cms and render arbitrary html code.
You can create a simple page in /themes/blackcandy/templates/MyPage.ss
This page template will be used to render any page type of the same name. Try creating a new class in /mysite/code/MyPage.php that extends Page. Then dev/build and view create a new MyPage page object in the CMS. That page should use the MyPage.ss template.