Zend same template for different modules - php

I am developing a system which has multiple user levels. However most of the time the views inside each modules should be the same.
For example a user view should be 99% identical to the admin view however an admin can have some small extras like delete buttons on user posts etc.
What is the best approach to not duplicate a ton of template view files within each module?
The best solution I can think of is using the _base module and putting the view files in there and inside them do an (if($user->isAdmin(): extra HTML bits) and have both the user module and admin module render the base module views?

If you are creating template file(layout.phtml) in your /module/Application/View/layout/ folder, then the layout will be applied for all the views.
If you want to disable layout for some specific view, you can use like this:
public function yourAction() {
$viewModel = new ViewModel(array(
'foo' => 'bar'
));
$viewModel->setTerminal(true);
return $viewModel;
}

Very first need to check if user is admin then in view you can call another view partially having extra code like delete button using $this->partial() helper.

Related

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

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.

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/
}
}

zf2 extend existing modules

I've been working with zf2 for a while but i could not get a solution for the following problem.
eg. we have a module called "Product" which handle the most of the product stuff.
Another module - let's call it "Review" - should extend the Product module and extend the product view to offer a product review to the user.
Until this point there seems to be no problem. We can easily overwrite the product view in the Review module.
Now the tricky part where i got stuck.
There's a third module - let's call it "Social". This module should provide social functioniality to the whole application.
This module should also modify the product view and add a link in the product page called "EMail to a friend".
Now my problem...
The product view is modified by the review module. If i overwrite the view in the Social module, the changes from the Review module are lost.
// Edit
Info: The Social and Review module should be able to modify the views WITHOUT modifiing the Product module.
Any tips, hints or ideas are welcome.
It depends what code you are trying to avoid duplicating.
If it's just HTML content you can create a custom ViewHelper to render the required HTML. This could then be reused within each view that needs the "social" content.
I suspect however you are reffering to the a 'social' controller action and you wish to reuse the return result of that within other views. If so, one soultion would be to use the forward() controller plugin.
From the documentation:
Occasionally, you may want to dispatch additional controllers from within the matched controller – for instance, you might use this approach to build up “widgetized” content. The Forward plugin helps enable this [by] returning the results of the dispatched controller action
This is useful in a situation like yours as you require the "Social" module to be an additional element of the view; rather than a replacement.
For example
// SocialModule\Controller\SocialController.php
// Controller action to display social buttons (twitter/facebook etc)
public function socialAction()
{
// Some controller logic...
// Returns the social button view
return new ViewModel(array('foo' => $bar));
}
// ProductModule\Controller\ProductController.php
public function productViewAction()
{
$product = $this->productService->find($this->params('id'));
// Displays the product 'view' page
$view = new ViewModel(array(
'product' => $product,
));
// Re-disptach the social module action and return it's view model
$socialView = $this->forward()->dispatch('SocialModule\Controller\Social', array(
'action' => 'social',
));
// We now have the view model, attach it to our view
$view->addChild($socialView, 'social');
// Return the aggregated view
return $view;
}
All then is required is to render the content in the view
// product-view.phtml
echo $this->social;

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.

disable layout for sfDoctrineGuardPlugin in Symfony

How could I prevent mentioned plugin's login form from using default layout? I am aware of this question, but that answer doesnt work for me. For starters, there's no signin module in modules dir, probably plugins handle it in different way, I dont know. Just learning symfony. Thanks in advance :)
For now its not possible to set custom layout for some sfGuardAuth action via custom view.yml.
This is how I did it.
This is my apps/backend/modules/sfGuardAuth/actions/actions.class.php:
<?php
require_once(sfConfig::get('sf_plugins_dir').'/sfDoctrineGuardPlugin/modules/sfGuardAuth/lib/BasesfGuardAuthActions.class.php');
class sfGuardAuthActions extends BasesfGuardAuthActions
{
public function preExecute()
{
$layout = $this->getActionName() == sfConfig::get('sf_login_action') ? 'sfGuardLayout' : $this->getLayout();
$this->setLayout($layout);
}
}
If you just want to set a different layout, you need to add a module (just create it manually) called "sfGuardAuth". Inside the /config/ directory for that, change the layout in the view.yml like for any other module. This is explained in:
http://www.symfony-project.org/plugins/sfDoctrineGuardPlugin/4_0_0
... under section "Customize sfGuardAuth module actions".
However, if you want to "embed" your login form on another existing page, you could turn the login into a component - which means it uses the existing layout of the page it occurs in.
Component action in a custom module:
public function executeSigninLightbox(sfWebRequest $request)
{
$class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin');
$this->form = new $class();
}
... which like all components uses a partial as its view. The partial now has access to $form like a standard login page. The partial for this would be called "_signinLightbox".
Hope that helps.

Categories