Very new to CI. Have completed only the initial tutorial and now want to customise the code.
My question relates to customising the example code once that tutorial is complete.
How can I define the current year once so that the footer template file can display the current year regardless of which controller is being invoked?
There is a header template which includes the following code: <?= esc($title) ?> (also, is the <?= syntax specific to CI?)
I understand the $title variable contains the value populated in $data['title'] within each of two controllers:
as $data['title'] = ucfirst($page); // Capitalize the first letter in Pages.php
as $data['title'] = $data['news']['title']; in News.php
and also as $data = ['news' => $model->getNews(), 'title' => 'News archive',]; in News.php (also - why is there a trailing comma here?)
The first customisation I would like to do is to put the current year into the footer template. To generate the current year, I am using date("Y").
I have reviewed one or two other questions here but cannot solve it.
From reading those answers, it seems I have many options:
Define a constant in the config file
Create "MY_Controller" which can extend CI_Controller, Controller or BaseController (?) then update all controllers in the app so they extend MY_Controller ... but how exactly do I use MY_Controller to populate $data['currentYear'] so that the footer template may reference it as $currentYear? (Or do I need to reference it a different way from the template?)
Create "MY_Controller" as above (with all the same questions) but put the file in application/libraries
... or in application\core
Create your own defines.php file and require that from index.php (? - there is no index.php in this example)
Create your own helper function (where to put it?) which can then be used "everywhere" in your code (but how? And when I tried using direct PHP in the footer file, it did not interpret the code - so how would you call a helper function from the footer template?)
I tried almost all of the above. I modified BaseController. I extended from BaseController to create my own controller then had all the other controllers extend my controller.
Obviously what I'm trying to achieve is to calculate the current year once so that the footer template can display it regardless of which controller is being invoked.
Related
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.
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/
}
}
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.
I can't understand when to use Layout's variables and when to use View's variables to get page segments on the page. Here is the picture form their Layout package tutorial ($this means the View instance everywhere):
Why Navigation, Content and Sidebar segments are got as Layout variables?
$this->layout()->nav;
But HeadTitle, HeadScript, HeadStylesheet are got straightly from View?
$this->headTitle(); // I know that this is a placeholder view helper.
// But this segment of the page logically belongs to Layout.
// and it has to be called smth like view->layout->placeholder
And why Header and Footer are from some partial method of the View but not Layout's properties?
$this->partial('header.phtml');
I've tried to change them and both ways work fine:
echo $this->nav; // I assigned navigation segment script to the View and it works;
I tried to assign Footer segment script to the Layout and it also works:
$layout->footer = $footer;
echo $this->layout()->footer; // it also works, it's displayed on the page
Any of the ways may be applied to any variable on the page. For example in Navigation segment I have a lot of variables to display and I can output them using both ways - one variable as Layout's property, another one sa View's property.
So what is the rule to use them right way? When should I use View's variables and when Layout's ones?
I agree that this isn't very clear from the documentation, and I don't think $this->layout()->nav is explained at all. A few points that might help:
$this->layout() is actually a call to the layout view helper, which returns the current instance of Zend_Layout.
Zend_Layout registers its own placeholder helper (with the key 'Zend_Layout'), and by default creates a 'content' variable in this.
the Zend_Layout class has a magic __get() method which proxies any member variable calls over to its registered placeholder container. So calling $this->layout()->content is another way of writing $this->placeholder('Zend_Layout')->content
the Zend_Layout class also has a magic __set() method that proxies stored data to the placeholder class. So $layout->footer = 'foo' is the same as calling $this->placeholder('Zend_Layout')->footer = 'foo' in the view
With that in mind:
Why Navigation, Content and Sidebar segments are got as Layout variables?
As these are accessing data stored in Zend_Layout's placeholder. You could also use $this->placeholder('Zend_Layout')->content
But HeadTitle, HeadScript, HeadStylesheet are got straightly from View?
These are view helpers.
And why Header and Footer are from some partial method of the View but not Layout's properties?
This is the standard way of accessing content from other templates.
In general, assume that using the view object is the correct way to access the data. Use the layout object/helper only if you know the data is in the layout placeholder.
The advantage of using placeholders over partials is that you can access and modify them in several different places, including in the view itself. For example say you had a sidebar which is stored in a partial. If you were to store this in the Zend_Layout placeholder instead (for example in a controller plugin), you can then override this for certain actions in the controller:
public function someAction()
{
$this->view->layout()->sidebar = 'Some other sidebar content';
}
or in the view script itself:
<?php $this->layout()->sidebar = 'Content for this page only'; ?>
Using Yii here. But perhaps a more general approach could fit:
Let's say we have a controller/view called: "Theater"
Inside Theater, we wish to have several static pages about several specific theaters displayed.
PLEASE CONSIDER THE FOLLOWING POINTS (that the DOCUMENTATION don't answer):
1) The static pages already share the same layout as OTHER views under this application
*Theater index.php* (that for example, can contain a cmenu with links to each specific theater) already suffers influence from main.php layout.
2) index.php have some HTML that ALL specific pages have.
Also, Theater index.php as some general html elements shared with all specific theater pages.
Question:
How should we render those specific static pages each time we select a given theater on the menu ?
My first thought was to somehow use the index.php view and inside, call $this->renderPartial('specificTheaterHere'); but I believe this approach doesn't work, because inside renderPartial we should have a variable, but where should we change that variable ? Should we have a controller method for each static page and pass there the variable of a partial view? Would this make sense?
I've read this nice article, still, the question remains.
http://www.yiiframework.com/wiki/22/how-to-display-static-pages-in-yii/
Note:
It makes no sense to create a LAYOUT for static pages here, because, they already share the main.php layout.
Thanks in advance
[update] Take a look at Yii layouts as well.
You don't need that, you can use the actions() method to render static pages. In a fresh Yii installation, you can find that in SiteController::actions().
In routes:
'page/<view:\w+>' => 'site/page',
In SiteController
public function actions() {
return array(
'page' => array(
'class' => 'CViewAction',
'layout' => 'my_layout',
),
);
}
Check the full documentation here
Now any static page in /views/site/my_static_page.php can be access by: http://mysite.com/page/my_static_page
Check the full documentation and tutorial here:
http://www.yiiframework.com/wiki/22/how-to-display-static-pages-in-yii/