Is there a way to override a component's output from a plugin? I'm aware of the concept of template overrides, but I just don't want to put the code into the template because
it simply feels wrong to put the code there because it is not a feature tied to the template. In my opinion the template should only contain overrides to fit core output into the template
I may want to add parameters to change behaviour (i.e. there is business logic involved and I want to keep it all together - in a plugin)
I may reuse it at another webpage and obviously don't want to copy & paste the code to all the templates I use
Or are my considerations wrong and the answer is: Just keep it simple and put it into the template?
I found a tutorial about Adding custom fields to core components using a plugin in the joomla documentation where the costom fields are addet to the view with a template override - which seems odd because the rest is done in a plugin - so i assume there is no way to to an output override from a plugin?
Related
Can I include a php file to tpl?
All I need is data from the database. I tried with required('path/file.php') but without success.
I already have controller for that. I need to include controller/file, class/php extension anything just need in tpl. I use OpenCart version 2.3.
if you need data from the database, in MVC a model should be used, and then let the controller add it's data to the $data array, which the is available to the template.
this shows how OpenCart actually works (source):
now tell me how your attempt to change a template (view) fits into there (notice the vertical lines).
just load the model into the controller and add results to $data, then it's available in the template.
You should not directly mix templates with custom PHP code, even if in some template engines it's possible.
You should rather look into means of providing your PHP logic through functions, filters or macros. This way you make your PHP code available in templates.
If OpenCart uses twig, you could look into how to do such in the documentation about extending twig here.
everyone.
I have to change the template of the FormHelper. Not for just 1 or 2 view, for ALL views. And i would like to do this without to have to write hundred of line in every view.
Is it possible to change the FormHelper default template ?
Where the default template is located ?
CakePHP documentation don't say anythings in order to change template of Whole Site, for FormHelper or Paginator, that's a real problem.
I'm not with CakePHP 3 but only CakePHP 2.6
If I understand correctly, you want to modify or use their own "bake" template, in order to avoid repetition of writing html and php code.
Here is what documentation say:
http://book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html#modify-default-html-produced-by-baked-templates
If you want to modify cake helper, copy from
\cakephp\lib\Cake\View\Helper
in the \cakephp\app\View\Helper
and then make modification.
Here is documentations for creating own helpers
http://book.cakephp.org/2.0/en/views/helpers.html#creating-helpers
Well...the file you are looking for is located in
/lib/Cake/View/Helper/FormHelper.php
You should search for example for:
function input(
or
function create(
that is the "template" you are looking for.
I think the easiest way to change the output is to update your $options array. There you can add classes or labels or whatever.
I am making a site with Joomla mainly because of the user levels. Now hands on into development enough that I cannot back out, I've realized that Joomla's theming is not as awesome as WordPress'.
When I started making the Joomla theme I realized there is no single.php like separate template for displaying article pages. But I want that when I click on an article link the page layout is wider so that the full article is more readable, basically I wanna hide the sidebar and make the article column wider when viewing a single article.
The biggest problem is that I have finished the HTML & CSS pre Joomla or any CMS jump with all the HTML5 tags and what not. I figured there would be template tags like WordPress or something similar that would give me complete control of my HTML but with Joomla everything just gets rendered as complete components with HTML and CSS all set in. It was such a pain when the pagination just rendered as a list when all I wanted was 2 links of previous page and next page. Am I doing something wrong or is Joomla just built this way.
If I start changing the components and modules the way I want, will they be changed back to defaults when I update the core. I read somewhere from 1.7 Joomla has integrated 1 click upgrades.
I sincerely hope I'm just wrong.
Sorry for jamming three questions in one but I think my title is quite explanatory so anyone looking for similar things would understand.
Thanks! I appreciate all the help.
and welcome to Joomla. Because your question(s) are long so will your answer so hold still...
First of all just to clear it up, the Joomla templating is a proper templating engine where as Wordpress is not (it's not even a CMS - check their website), this is often construed as poor coding rather than best practice as it's more of a steep learning curve.
1) I wanna hide the sidebar and make the article column wider when viewing a single article.
This can be done through conditional assignment within your template, if around your RH column in your template you can place this if statement:
if (JRequest::getString('option') != 'com_content' && JRequest::getString('view') != 'article') { echo '<div id="myRightHandColumn"><jdoc:include type="modules" name="right" style="xhtml" /></div>'; }
You will also need to apply this code to add a class to your wrapper around your main content to tell your CSS to make it wide.
2) give me complete control of my HTML but with Joomla everything just gets rendered as complete components with HTML and CSS all set in
That's not entirely true, all WELL made extensions have a views folder containing overridable html output. For example if you wished different HTML to be outputted for the article view from com_content you would copy this file:
/components/com_content/views/article/tmpl/default.php
and place it in your template like so:
/templates/your_template_name/html/com_content/article/default.php
3) If I start changing the components and modules the way I want, will they be changed back to defaults when I update the core.
Assuming you follow my instructions and use overrides rather than core-hacks then you will not lose changes when upgrading the core. Naturally you should never upgrade in a production environment.
If any parts are unclear just drop a comment and I will help as best I can.
You are definitely doing it wrong, Joomla's templating system is far more advanced and flexible than WP. You need to read up on the template override system and how to use collapsible module positions. The official documentation is still 1.5 focused, but it's pretty much the same thing as far as overrides and modules are concerned.
Overrides - http://docs.joomla.org/Understanding_Output_Overrides
Collapsing columns - http://docs.joomla.org/Collapsing_columns
All template docs - http://docs.joomla.org/Template_Development
You should also look in to the new template style options.
How does Drupal 7 render out a page? What's its equivalent to an MVC's view system.
When it comes to the rendering out the final HTML page for a request, most PHP frameworks (MVC based) I've worked with take an approach where a top-level layout/page PHP file sets out the basic document structure, and then renders our various sub-views via includes or view rendering methods.
//Simplified version
Page.phtml
Head.phtml
Body.phtml
Banner.phtml
Topnav.phtml
Left.phtml
Content.phtml
Footer.phtml
I'm a little confused as to Drupal's take on this. I'm reading through Pro Drupal Development, and it starts off in similar territory with a page.tpl.php. However, it glosses over how the theme engine (is that the right term?) gets the various parts of PHP into this page (not a criticism, the book's taking an approach that different from the path I'm on).
Also, the Drupal 7 themes don't seem to have the page.tpl.php file, so its not clear (to me) where the page skeleton comes from. Also, from what I've read it seems like "Blocks" are involved, but it's not clear to me if "Blocks" makeup the entire page, or if blocks are something used selectively by themes.
So, working from high level concepts (or get as detailed as you'd like), how does Drupal 7 render out a page?
I realize you can, and probably should, start with Drupal without understand how everything ties together. I'm specifically trying to learn how the various Drupal systems come together. Apologies to people tired of reading this disclaimer!
I think there are two questions here. First, how does a single theme/template get rendered/used/displayed and second, how does the whole site come together. I think the second question has already been answered above, so I'm going to try to explain the first a bit more.
First, a module (that's why system.module exists, for all these stuff that only a module can do like implementing hook_menu()) needs to define that a specific theme function/template exists, by declaring it in hook_theme()
Speaking of that, there are two different things which can be used. A theme function, which is a function prefixed with theme_. Often used for smaller elements of a page wich have more complex logic/PHP like theme_table(). And a template, which is a file with the tpl.php like page.tpl.php
To use a theme function/template, you can either call theme() like this:
$output = theme('table', array('rows' => $rows, 'header' => $header));
Or you can use the new, so called renderable array thing. That is basically an array of data + information which theme to use:
$output = array(
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
);
The second is preferred because it means that it will be themed as late as possible and other modules can change it in hooks. The result will be exactly the same, drupal_render() will then call theme() itself during the final rendering.
When theme() is called, it looks up the function/file to use, looks if it has been overriden the used theme, if there are so called template suggestions and then uses them.
To override a theme function in a theme, copy it to your template.php file and replace "theme_" with "yourthemename_", if it is a tpl.php file, copy it to your directory.
Now, what the final rendering process is doing is basically building up a large $page array, that is a renderable array (Some documentation of that is in hook_page_alter() and then call drupal_render() on it.
The global structure of the page/template hierarchy (which is not hardcoded, but given through whatever is in $page) is something like this:
html.php.tpl
head.php.tpl
page.php.tpl
multiple regions
multiple blocks
Almost everything is a block in Drupal 7, including the actual content, which is typically a node. A theme defines which regions it has and then you can assign blocks to it in the UI.
When trying to understand how the templates fit together, the best tool I've found is the Theme Developer module. It works a little like Firebug and allows you click on areas of a page to see the theme functions or template files that are used for various parts of the page, along with the "suggestions" that can be used for overriding them.
Templates can fit together in a variety of ways. Drupal does make some assumptions about how they'll be nested, which are reflected in the default variables that are available in the templates files. However, both the template suggestions and the available variables within them can be modified.
As I understand it, page.tpl.php has been replaced by html.tpl.php in Drupal 7.
The best description I've heard for "Blocks" is that they're what you use to display content that will be re-used in various areas site. The most common use is for sidebars, such as "Recently updated pages" lists or "Who is online now" lists.
Try take a look a presentation on How pages are built on Drupal 7 from drupalcon at http://sf2010.drupal.org/conference/sessions/page-render-drill-down-drupal-7 specially after 5 minutes from start (05:10). Sorry can't specify much detail here cause i still watch it my self. And try to understand it as well. ;)
[Update]
After watch the presentation here is my quick conclusion on how drupal 7 render out a page:
Instead of common approach that you mention from your question
where a top-level layout/page PHP file
sets out the basic document structure,
and then renders our various sub-views
via includes or view rendering methods
Drupal render a page through a rendering flow approach like this
bootstrap
menu_execute_active_handler
page_callback
delivery_callback
hook_page_alter()
drupal_render($page)
However, it glosses over how the theme
engine (is that the right term?) gets
the various parts of PHP into this
page (not a criticism, the book's
taking an approach that different from
the path I'm on).
that various parts is served starting from rendering flow number 3 (page_callback) through flow number 6 (drupal_render($page)) where its start to return an array of drupal render-able array and then latter in your theme you can use the $page variable (served from drupal_render($page)) to render i.e drupal content.
I am trying to theme node page based on content type. Here I tried to follow the instructions as in http://drupal.org/node/249726.
What I did , copied node.tpl.php file to my theme directory. Renamed it as page-node-mycontenttype.tpl.php and wrote preprocess function in my template file as shown in above link.
Apparently found that It displays only the Node contents but not the common layout elements(HTML) (logo,header, footer and sidebars etc. which is defined in page.tpl.php).
So is it necessary for me to define same common layout elements(HTML) once again (those defines in page.tpl.php) in page-node-mycontenttype.tpl.php?
If so then I have to manage 2 template files. Any HTML changes need to be done twice in both template files.
Is there any better way of having common layout template file referred by both page and node content type only the middle content area is coming from two different files (page or node content type)?
Could you please suggest common practice and way to achieve this?
Note: Make sure that to override node template file with respect to content type then both template files node.tpl.php and node-[content_type].tpl.php file need to be in your theme directory.
The file you're looking for is node-mycontenttype.tpl.php . You want a node template, not a page template. If you rename page-node-mycontenttype.tpl.php to node-mycontenttype.tpl.php, you'll get the result you're expecting.
The page template (page.tpl.php) in Drupal defines the overall markup and where the contents of the various regions will be outputted. Traditionally it includes the logo, menu, footer message, and a few other static elements. The Drupal theme system allows you to override the entire page template for a specific node type (which is what you ended up doing), but based on your explanation, what you're really trying to do is override just the node template.
Further reading:
Explanation & diagram of what files control which pieces of output: http://drupal.org/node/171194
General documentation on overriding output in the theme system: http://drupal.org/node/341628
When reading the above, make sure to note the version of Drupal that it applies to (there's a tag up at the top of each page). Things changed fairly dramatically between 5 and 6 and have shifted again (though not nearly as much) between 6 and 7.
t-dub is right about node-contenttypename.tpl.php.
However, if you want to retheme the entire page for a given content type (maybe a landing page), you have two options (there are likely other options but these are the ones I've discovered.
You may use a module called sections and then create a theme for each of the different content types.
OR
You can add the following function to your theme's template.php:
function templatename_preprocess_page(&$vars) {
// Template change based on node->type
if (isset($vars['node'])) {
// Add template naming suggestion. It should alway use hyphens.
$vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
}
}
Just swap out templatename (in the function name) for the name of your template. Then you can create page-contenttypename.tpl.php for a unique design for nodes of that content type.
I've used sections in the past, but I prefer using the method described in #2 because it feels more drupal-ly. Its also a lot simpler to maintain.
Hope this helps someone!