I read this and this.
But i couldn't find the answer.
I have this file, where my action's views exist:
View/MyController/index.ctp
I also have this file
View/Commons/blocks.ctp
Blocks.ctp file includes these:
$this->start('sidebar1');
echo 'Some content';
$this->end();
$this->start('sidebar2');
echo 'Some content more';
$this->end();
So in the "index.ctp" file i want to fetch sidebar1 or sidebar2.
How can i do this?
I wrote this to index.ctp, but didn't work.
<?php echo $this->fetch('sidebar1'); ?>
Also this one didn't work
<?php echo $this->fetch('../View/Commons/blocks.ctp/sidebar1'); ?>
Thank you
Put at top of your index.ctp:
$this->extend('Common/blocks.ctp');
With blocks and view inheritence, you can create "sub layouts" which are basically analogous to the standard Cake layout file. So you'd have the main layout.ctp, and the controller-action view ctp would be based on a parent view file (e.g. /Commons/xxxx.ctp) which is "populated" via blocks.
Blocks are like elements but less "formal" unless you use the "view inheritance" features. Their markup & data get created in your scripts, in possibly multiple locations so they can be more cumbersome to debug/maintain (i.e. imagine appending markup to a block from multiple classes). They're also harder to reuse if you don't use inheritance.
Elements are more like stand-alone view files that can be used within any controller+action view or layout: all the markup is in one place and you just pass in the data.
Bottom line: if you're new to Cake, you can get by fine with just elements. View inheritance can help make views/layouts more elegant but at the price of some complexity.
Related
I updated views in drupal 7 and am getting rogue "?>" symbols rendering on my pages for regions I created in a zen sub theme.
The code for one of the regions follows:
<!--/#adbanner-->
<?php if ($page['adbanner']): ?>
<div id = "adbanner" role = "banner">
<?php print render($page['adbanner']); ?>
</adbanner><!-- /#adbanner-->
<?php endif; ?>
Okay, let's try to debug this:
Start by deleting all of your content from your files (do it with a ctrl + a, ctrl + x and then paste it into a new file - this way we can be sure that ALL of the content is gone). Save the files and render the page.
If the symbols continue to pop up, it is an encoding marker that you will have to turn off. If they stop, it is not an encoding issue and we will have to try something else.
NOTE: When I say delete all of the content, make sure that you keep the structure, i.e. the calls to load any external files. Just make sure that the visible content of those files is removed.
So I gather I can render a specific element of the $page['content'] array like so...
<?php print render(field_view_field('node', $node, 'field_image')); ?>
Where out of the box this will render the element as expected with standard defaults. Ok, so how can I make sure that hand plucked element no longer renders in the <?php print render($page['content']) ?> call later?
Why do I want something dumb like this? Because every page WILL have a header image with a few css tricks for overlays, design and such. But not every page will have attachments, links, and so on... you know, things that are additional fields in the page. So I can't manually print out each field since I don't know how many or what else there is. All I know for sure is the field_image I'm printing above is wrapped in a ton of markup for styling and must be done this way. Same for a few other fields.
Basically I'm looking for a way to unset the field immediately after use.
Does anyone know how to achieve this? I'd rather not make a view or a custom block that displays for specific pages. I eventually have to hand this over to a client who will not be able to wrap their heads around a single page being administered over many places in the CMS.
You can in fact control the display of individual fields for content types in Drupal without having to resort to the function you've used.
Since you know in advance which field(s) you want to suppress, you can turn off its display in the content type settings.
In Drupal 7, see:
Admin >> Structure >> Content types >> your content type >> Manage display
Under "Format" select < Hidden > for the field that you want to omit.
This will prevent the field contents from being displayed within the usual node contents, but field_view_field will naturally still work.
You can also fine-tune your field formats based on different view modes, e.g. choose to display the field within teasers but not in full content.
Looks like this will work:
<?php
print render(field_view_field('node', $node, 'field_image'));
MYTHEME_remove_item($page['content'], 'field_image');
?>
and in template.php file I made this function:
function MYTHEME_remove_item(&$content, $field)
{
foreach($content['system_main']['nodes'] AS $key => $val){
unset($content['system_main']['nodes'][$key][$field]);
}
}
This is ridiculous, in my opinion. I would think a system as robust as drupal would have a solution for something like this. If anyone knows the proper way to do this I will gladly mark them as correct. In the meantime, for others facing similar situations, this worked for me.
I have a requirement to perform an indexed search across content which must include a couple of tags in the result. The tags must be a random selection. The platform is Drupal 7.12
I have created a view that manages the results of a SOLR search through the search_api. The view returns the required content and seems to work as intended. I have included a couple of Global: custom text fields as placeholders for the tag entries.
I am now looking for a solution to manage the requirement to randomise the tag values. The randomisation is not the issue, the issue is how to include the random values into the view result.
My current approach is to write a views_pre_render hook to intercept the placeholders which appear as fields ([nothing] and [nothing_1]). The test code looks like the following
function MODULE_views_pre_render( &$view )
{
$view_display = $view->display['default'];
$display_option = $view_display->display_options;
$fields = $display_option['fields'];
foreach( $view->result as $result )
{
$fields['nothing']['alter']['text'] = sprintf("test %d", rand(1,9));
}
}
I am currently not seeing any change in the placeholder when the view is rendered.
Any pointers to approach, alternate solutions etc would be gratefully received as this is consuming a lot of scarce time at the moment. Calling print_r( $view ) from within the hook dumps over 46M into a log file for a result set of 2 items.
There are two possible solutions for your task.
First approach is do everything on the template level. Define a template for the view field you want to randomize. In advanced settings of your display go to Theme: Information. Make sure that the proper theme is selected and find the template suggestions for your field. They are listed starting from most general to the most specific and you can choose whatever suits you better.
I guess the most specific template suggestion for your field would be something like this: views-view-field--[YOR VIEW NAME]--[YOUR DISPLAY NAME]--nothing.tpl.php. Create the file with that name in the theme templates directory and in this template you can render what ever you want.
By default this template has only one line:
print $output;
you can change this to:
print sprintf("test %d", rand(1,9));
or to anything else, whatsoever :)
Second approach is to go with Views PHP module. WIth this module you can add a custom PHP field in which you can do whatever you want. Even though the module hasn't been released it seems to work quite well for the most of the tasks and most certainly for such a simple task as randomizing numbers it will work out for sure.
I stumbled upon this while searching for another issue and thought I would contribute.
Instead of adding another module or modifying a template, just add a views "sort criteria" of "Global: Random".
I have a fresh CakePHP 1.3 install and it currently has one layout. I am about to add a few more but I don't want to have to keep coping and pasting the Header and footer in to each layout.
At first I thought that I could do this with an Elements, but it does not seem to render the Configure::read('var_name'); chunks while in an element.
My other thought was to create a common layout and use lots of variables to add and remove sections from the screen depending on what type of user they are... but this would be troublesome to say the lest.
My question is:
Is a way to include a header/footer section in to a layout while getting the Configure::read() function to output text?
I still think that elements are the right way to go for this (shared view snippets, FTW). I have to admit that I'm a little surprised that elements can't read from the Configure class, but I'll concede that I haven't tried it. If that really won't work, then try passing the values directly to the element:
<?php echo $this->element( 'partial_name', array( 'var_name', Configure::read( 'var_name' ); ?>
In the element, you should then be able to access the variable simply as $var_name. For more on passing variables to elements, take a look at the [Passing Variables into an Element](Passing Variables into an Element) section of the element documentation.
Hope that helps.
Create element with new header suppose new_header.ctp. Then put element('new_header')?> in your preferred position layout
What is the best way to create regions in your layout similar to Wordpress's Widgets or Drupal Blocks? What is the best practice method of doing that in CakePHP?
If by regions you mean a special "content container" (never used WP/Drupal), then it's very easy.
There are several ways to accomplish this, but the one that came to my mind first was this:
Create a helper (or an entire plugin) to handle the "which content goes into which container" logic. Shouldn't be too hard to do because you have many Cake utility classes to help you out with that (such as the Configure class). This should obviously be configurable by the end user.
Create containers in your layout, example:
<div class="content-container" id="content-container-left">
<?php echo $yourHelper->outputContent("left"); ?>
</div>
Two options:
Content should be based on elements; or
Content should be based on custom plugins (which actually do their stuff and output the content)
Note: There are probably better ways to accomplish what you want, this is just the first that came to my mind. I'd recommend some pencil-and-paper planning before you actually code anything, it will improve your chances of finding the best way for your app.
I created a Sidebar Helper recently that you might find useful.
You define the content of the boxes in Cake elements, and then add them by calling ...
$sidebar->addBox(array('element'=>'my_sidebox_element');
... this would render the content of views/elements/my_sidebox_element
Alternatively you can specify te content of a box 'inline':
$sidebar->startBox(array('title' => 'My Inline Box'));
<p>blah <b>blah</b> <span>blah</span></p>
$sidebar->endBox();
The in your layout file call
echo $sidebar->getSidebar();
... and each of your boxes will be rendered as divs
Technically speaking this doesn't need to be used as a 'SideBar' - it ultimately depends on how you render the layout with CSS.
See the documented code for more details:
SidebarHelper on GitHub