silverstripe: use another "root" - template instead of "Page.ss" - php

Is it possible to render another "root" template instead of the Page.ss file for some specific pages / controllers? There are already some pages using the Page.ss template, but now there will be a new "Intranet" section on the website where the pages should have another "root" template: IntranetPage.ss.
Page.ss should stay as is and should not be touched at all.
I mainly want different "root" templates because both templates load different JS and CSS files. Also the "container" HTML is quite different.
I was able to create a custom controller which does manually what I need. Something like this:
class IntranetPageController extends PageController
{
public function index()
{
return $this->customise([
'Layout' => $this->renderWith(['Intranet/Layout/IntranetPageLayout'])
])->renderWith(['Intranet/IntranetPage']);
}
}
The code is inspired from here: https://docs.silverstripe.org/en/4/developer_guides/templates/rendering_templates/
IntranetPage.ss is used now as the "root" template. IntranetPageLayout.ss is displayed for the $Layout placeholder.
That seems to work, however I have many pages which have to be based on IntranetPage.ss. It feels strange to write for every new Controller the very same index function (with a small adjustment to load another LayoutPage).
I am sure, Silverstripe has some convention to do that automatically :)
What I need is very close to having a individual theme per page, but I am not sure if that is possible...

Instead of extending PageController, extend your IntranetPageController in new controllers. Whenever index is called, it will call your index function from your parent class, in your case IntranetPageController.

Related

How to create 'blog' and new page in Fuel CMS with CI

I am just starting out with fuel CMS and Codeignitor. I'm looking for easy to read suggestions, references, tutorials, code snippets, ANSWERS etc for the 2 following questions below. (2-part Question)
1.) How do I access 'blog' functionality; I've read it is built in as a /view/blog.php but I don't see it; I've tried to create my own (in same directory) but it simply resolves as a static page (I created it from the dashboard) but it lacks any blog > post > get post functionality; like 'blogs' do. I've read time over, like Wordpress and Drupal; Fuel has a 'blog' template. There is none under 'layouts' as well.
So, at this point, I wouldn't mind creating my own 'blog' page - Which leads to:
2.) How do I create a new page manually in Fuel CMS, without the dashboard.
I've created an empty .php file in this directory per documentation:
C:\xampp\htdocs\FUEL-CMS-master\fuel\application\views
I don't really need a custom _variables/ with this -- so what am I missing. I've read I don't need to add / set a new controller with this type of page nor static pages. I also don't want to have to do anything with the controller if I don't need to.
Codeigniter works on CMV Controller - Model - View so to create a simple page you need to create at least 2 files 1 controller and 1 view
if you are using CI 2.2 http://www.codeigniter.com/userguide2/overview/at_a_glance.html
if you are using CI 3 http://www.codeigniter.com/user_guide/overview/at_a_glance.html
first you need to create controller
second create your view
create a file in application/controllhers/blog.php
<?php
class Blog extends CI_Controller {
public function view($page = 'home')
{
//you can acesse this http://example.com/blog/view/
}
public function new($page = 'home')
{
//you can acesse this http://example.com/blog/new/
}
}

Style admin page in Yii

Im here again with a question about yii framework.
I've got a page under views/myviewname/admin.php.
I've got a page under views/myotherviewname/admin.php.
Now i want to give those pages another style. But how do i do that?
I've created a page under themes/classis/views/myviewname/admin.php and in that file i got this:
<?php /* #var $this Controller */ ?>
<?php echo $content; ?>
But i get an error. Because $content is not defined.
How do i style those pages? Would be nice if i can style all admin pages at once.
First of all, this is undeniable that $content variable will be known as undefined, since it can only be used in Layouts, not Views.
As you probably know, if you already have set a theme for your application(in main config file by 'theme'=>'myTheme'), Yii looks for that into themes/myTheme and all views will be rendered in themes/myTheme/views/x/y.php instead of views/x/y.php. Also, your layouts will be overridden by layouts located into themes/myTheme/layouts.
Now, lets assume that we want to create 2 themes:
DarkTheme
LightTheme
We should create structures like below:
+themes
+darkTheme
+views
+layouts
+main.php
+myLayout1.php
+myLayout2.php
+myController
+myView1.php
+lightTheme
+views
+layouts
+main.php
+myLayout1.php
+myLayout2.php
+myController
+myView1.php
We have a main.php which holds our base theme structure(skeleton), and 2 layouts named myLayout1.php and myLayout2.php respectively. Also we already defined a default layout into our base controller(Usually Controller.php) like below:
public $layout='//layouts/myLayout1';
Now, we have a main layout which shows everything inside myLayout1 by default. We can change layout in out action like below:
$this->layout="myLayout2";
Also we can change application theme like below:
Yii::app()->theme="lightTheme";
Note: Theme name is case-sensitive. If you attempt to activate a theme that does not exist, Yii::app()->theme will return null.
Above codes can be written into beforeAction() method or every action. Please note that, if you render myView1($this->render('myView1')) and if the theme is set to darkTheme, Yii will render themes/darkTheme/views/myController/myView1.php instead of views/myConteoller/myView1.php.
To be more clear, $content will be used in layouts. Also, this is remarkable that, $content will be replaced by everything inside a view. So if you want to modify the whole page's schema, you must modify main.php layout. In front, if you want to modify the style of a view's content, you need to modify your layout.

Where can i save my block in cakephp directory structure that will contain all my menu links?

I want to create my custom theme in CakePHP. Where can I save my block in CakePHP directory structure that will contain all my menu links? And how can I fetch the file from View\Themed\MyTheme\Layouts\default.ctp?
Either use Elements (if your navbar shows on every single view) or use View Blocks (if navbar only shows on some views).
In your default.ctp file you would just do..
echo $this->element('navbar');
Which would render Views/Elements/navbar.ctp onto Views/Layouts/default.ctp (if you are using the default layout).
To access: View\Themed\MyTheme\Layouts\default.ctp, in (Cake 2.1+), you must tell cake which theme you want to use like so:
public $theme = 'MyTheme';
// or override in an action:
$this->theme = 'MyTheme';
Then:
$this->layout = 'default';
will refer to your View\Themed\MyTheme\Layouts\default.ctp.
If cake can't find the requested view file in MyTheme, it will fallback to app/View to find it.
In this way you can override views in your theme as needed.
So if you place your menu in View/Elements/menu.ctp, all of your themes will be able to access it. if you want to overwrite it for MyTheme, simply create an Elements/menu.ctp within MyTheme.
The process is slightly different with previous versions of cake if I recall.
Just place it under /App/Views/Layouts. If you want to use it everywhere, call it default.ctp, it will automatically be used. Otherwise, give it a different name, and then in the controller do:
public function some_action() {
$this->layout = 'mylayout';
}
This will display some_action using your layout instead of the default.

Making a controller for home page

Can you tell me how to use a controller for home page because i'm trying to put a model's data in home.ctp (homepage view) with
<?php $this->user->find() ?>but it returns
Notice (8): Undefined property:
View::$user [APP\views\pages\home.ctp,
line 1]
You should check out the cookbook; it has some solid CakePHP tutorials, at http://book.cakephp.org/
You haven't really provided alot of information, but my guess is your Controller uses a model 'User', and you're putting $this->user->find() in your view, when it should be in your controller. In your controller's action you'll want/need to do something like this...
Users_Controller extends AppController {
function index() {
$arrayOfUsers = $this->User->find(...);
$this->set('users', $arrayOfUsers);
}
}
You can then - in your View - access 'users' like so:
pre($users);
... since you used the Controller method set() to send a variable $users to the view.
All you really need to do is create a new controller if that's the direction you want to go. If this is the only statement you have that requires data access, it might be worth faking it in only this method of the PagesController. For example, one of my projects' homepages is 99% static save for a list of featured events. Rather than move everything out to a new controller or even loading my Event model for the entire PagesController (where it's not needed), I just applied this solution in PagesController::home():
$attractions = ClassRegistry::init ( 'Attraction' )->featured ( 10 );
Works great. If your page is more dynamic than mine, though, it may well be worth routing your homepage through a different controller (one that is more closely related to the data being displayed).
The default controller for the home page i.e home.ctp view, is located in /cake/libs/controller/pages_controller.php. This controller can be overriden by placing a copy in the controllers directory of your application as /app/controllers/pages_controller.php. You can then modify that controller as deem fit i.e. use models, inject variables to be used in the home page etc

Making static pages in Symfony 1

Im new to symfony and have some simple questions. I am trying to understand the module system, but I dont understand how I create the actual homepage or other pages that are not based off of a model from the db. For example, the simple about page that has static info or the homepage that is a combination of a bunch of information from different models.
Can anyone help?
First of all, modules do not have to be restricted to a model from the database. You can have a Foo module which relies on no database content, and a Bar module that is primarily based on 3 different models. The module separation is a way to logically break up your site into manageable sections. Eg an e-commerce site might have a Products module, a Categories module and a Cart module and so on.
Your last sentence can then be split into 2 parts:
1) Static information can be on any page - if it's for things like "About us" and "FAQ" etc, I personally tend to use a "default" or "home" module, and create the various actions in there vis:
./symfony generate:module appname home
and
class homeActions extends sfActions
{
public function executeAbout(sfWebRequest $request)
{
// ...
}
public function executeFaq(sfWebRequest $request)
{
// ...
}
}
with the corresponding template files (aboutSuccess.php, faqSuccess.php).
2) A page can be comprised of data from many different models - just use your preferred ORM's method of retrieving data and set it to the view ($this->data = MyModel->findByColumn(...) etc). If you mean data from different modules, then you'd probably be better off looking at partials or components for elements of a page that can be used across different modules (navigation etc). See the Symfony docs for more details on these.
I'm used to handle static pages in this way.
First I create a new entry in apps/frontend/config/routing.yml:
page:
url: pages/:page
param: { module: page, action: index }
Then I write a "page" module (apps/frontend/modules/page/actions/actions.class.php):
<?php
class pageActions extends sfActions
{
public function executeIndex()
{
$this->page = $this->getRequestParameter("page");
$this->forward404Unless($this->_partialExists($this->page));
}
protected function _partialExists($name)
{
$directory = $this->getContext()->getModuleDirectory();
return (is_readable($directory.DIRECTORY_SEPARATOR."templates".
DIRECTORY_SEPARATOR."_".$name.".php"));
}
}
Last step, put in modules/page/templates/indexSuccess.php this code:
<?php include_partial($page); ?>
So all you have to do from now is to create a partial for each static page ie.
apps/frontend/modules/page/templates/_home.php which you can reach at
http://yousite/pages/home (without the need to add a new routing entry for every page)
You can create a module, e.g. called static and create actions for every static page or only one action that delivers the page depending on a request variable. The only thing this action does is loading a template.
IMHO it would be good if symfony comes with a default module for this.
For example actions of (my custom) module static:
class staticActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
if(!$request->hasParameter('site')) {
return sfView::ERROR;
}
$this->site = $request->getParameter('site');
}
}
With this template:
//indexSuccess.php
<?php include_partial($site) ?>
The actual statics sites are all partials.
In my routing.yml looks like this:
# static stuff
about:
url: /about
param: {module: static, action: index, site: about}
This way you only have to create a new partial and a new routing entry when you add a static site and you don't have to touch the PHP code.
Another way to serve static pages without having to write any controller code is to set up the route something like the following:
myStaticPage:
pattern: /pageName
defaults:
_controller: FrameworkBundle:Template:template
template: MyBundle:Home:pageName.html.twig
Then just create your twig template and it should work fine.
Apart from the above, consider having a CMS for static pages, so you won't need technical savy people to mantain them or change them. This depends on the project, of course.
For really static and independent pages you can simply create any file in [pathToYourProjectRoot]/web directory.
It may by i.e. [pathToYourProjectRoot]/web/assets/static_html/about.html.
Then link to the page directly by http://your.site.com/assets/static_html/about.html.

Categories