This is the .ss template file:
<ul>
<% control Menu(1) %>
<li>$MenuTitle</li>
<% end_control %>
</ul>
<div>
<h1>$Title</h1>
<div>Date : $Date.nice</div>
$layout
$Content
</div>
This is the php file:
class ArticlePage extends Page {
static $db = array(
'Date' => 'Date',
);
private static $has_one = array(
'SingleImage' => 'Image'
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new DateField('Date'), 'Content');
$fields->addFieldToTab(
'Root.Upload',
$uploadField = new UploadField(
$name = 'SingleImage',
$title = 'Upload a single image', "Content"));
return $fields;
}
}
The date and image are not showing up on the page.
This could be any number of things.
It could be that you have not set the page type properly in the CMS.
It could be that you have not flushed your template cache.
To do this append ?flush=all to the end of your URL.
It could be that you have not done either of the above and have not completed a database build yet either.
To do this visit yoursite/dev/build.
It could be that you have not named the template correctly. It must mirror the php Class name (ArticlePage.ss)
It could be that you have not put this in the correct place. It must be in a templates folder in either the selected theme or your project folder. A flush is required after relocating the file (see above).
If you are using a theme it could be that you do not have the correct theme selected. See the documentation on this.
It could be because you're not being diligent with your code. The dot method for formatting a date nicely is .Nice, not .nice. $Date.asdf would also cause nothing to show up - as you are describing. Template variables fail silently if there is no sensible value to output.
Similar to the above - the layout variable is $Layout, not $layout (although this is irrelevant to this particular problem). $Layout also only works in 'main' templates (in the base 'templates' folder, not in the 'Layout' subfolder).
If all of the above checks out, then it could be simply that you have not put any information in through the CMS yet. You must of course complete this step before anything will show up.
Related
Is posible to save SS template variable in database from CMS and after execute it in template?
Okay lets see example:
In CMS i have settings where i put social media links and contact informatios.
Also in CMS i have module where i create HTML block-s which after that i loop in website.
In that html block i want to put existing $SiteConfig.Email variable.
I Try that but that is rendered in template like $SiteConfig.Email not show real email?
Is this posible to do or i need some extra modification?
Check photo
The question you have written makes no sense to me, but I understand the screenshot.
So, SilverStripe renders .ss files with a class called SSViewer. Basically it reads the file as string and then runs it through SSViewer to generate the HTML output.
But, as you saw, the output of variables is not processed.
I can think of 3 ways to get what you want:
Run the variables through SSViewer aswell (in this example, use $RenderedHTMLContent in the template)
class MyDataObject extends DataObject {
private static array $db = [
'Title' => DBVarchar::class,
'HTMLContent' => DBText::class,
];
public function Foobar() { return "hello from foobar"; }
public function RenderedHTMLContent() {
$template = \SilverStripe\View\SSViewer::fromString($this->HTMLContent);
// using $this->renderWith() will allow you access to all things of $this in the template. so eg $ID, $Title or $Foobar. Probably also $SiteConfig because it's global
return $this->renderWith($template);
// if you want to add extra variables that are not part of $this, you can also do:
return $this->renderWith($template, ["ExtraVariable" => "Hello from extra variable"]);
// if you do not want $this, you can do:
return (new ArrayData(["MyVariable" => "my value"]))->renderWith($template);
}
}
Please be aware of the security implications this thing brings. SilverStripe is purposely built to not allow content authors to write template files. A content author can not only call the currently scoped object but also all global template variables. This includes $SiteConfig, $List, .... Therefore a "bad" content author can write a template like <% loop $List('SilverStripe\Security\Member') %>$ID $FirstName $LastName $Email $Salt $Password<% end_loop %> or perhaps might access methods that have file access. So only do this if you trust your content authors
Use shortcodes instead of variables. But I never liked shortcodes, so I don't remember how they work. You'll have to lookup the docs for that.
Build your own mini template system with str_replace.
class MyDataObject extends DataObject {
private static array $db = [
'Title' => DBVarchar::class,
'HTMLContent' => DBText::class,
];
public function Foobar() { return "hello from foobar"; }
public function RenderedHTMLContent() {
return str_replace(
[
'$SiteConfig.Title',
'$SiteConfig.Tagline',
'$Title',
'$Foobar',
],
[
SiteConfig::current_site_config()->Title,
SiteConfig::current_site_config()->Tagline,
$this->Title,
$this->Foobar(),
],
$this->HTMLContent
);
}
}
After migration, every page add password textfield at the end of every page.
What is this? How can I remove or troubleshoot?
Drupal 7.27 with apache 2.4 and php 7.0 (same problem with php 5.6.35).
Look there is a script loaded on these pages (just above the <form> itself) that creates the input tags and set the windows focus in it :
<script type="text/javascript">
var d = document;
d.write("<br><br><form method='post'><center><input type='password'...>...");
// ...
</script>
You want to remove this script.
Since there are several ways to include javascript with Drupal it may be difficult to spot the code responsible for that. Given the ugliness of the script itself, it could very well be harcoded in a theme template file (in this case, theme switching during migration would explain why your issue suddenly arose).
The chance is that ugly snippets like this is quite often hardcoded so you can make a search for a part of the js string (e.g. 'd.write("<br><br><form') in your project at the root of your site and/or in sites/all.
Lastly, find the guy that wrote this and beat him ;)
Your code is including a java script in every page which is creating input type password since it is included in every page that's why you are getting this field.
kindly check your requirement for same.
In drupal We can add JS in drupal by following method
1.)By drupal_add_js() function
drupal_add_js() is drupal api function to include js.
Example:
drupal_add_js('misc/collapse.js');
// add JS file
drupal_add_js('misc/collapse.js', 'file');
// For including inline javascript
drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
//For including inline javascript and includ and includ it in footer
drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', array(
'type' => 'inline',
'scope' => 'footer',
'weight' => 5,
));
//For including External JS
drupal_add_js('http://example.com/example.js', 'external');
//For passing php value to JS
drupal_add_js(array(
'myModule' => array(
'key' => 'value',
),
), 'setting');
Example:
drupal_add_js(drupal_get_path('module', 'mymodule') . '/mymodule.js');
for more infomation visit https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_js/7.x
2.)Adding by Form API
we can used '#attached' property of form api for including js
Example:
$form['#attached']['js'] = array(
drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
);
3.)Adding JS in info file
We can including javascript in script file
Example:
name = My theme
description = Theme developed by me.
core = 7.x
engine = phptemplate
scripts[] = mytheme.js
4.)By preprocess function
if we want to conditionaly include JS we can include it in preprocess function
function mytheme_preprocess_page(&$vars, $hook) {
if (true) {
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/mytheme.js');
$vars['scripts'] = drupal_get_js(); // necessary in D7?
}
}
Is there a way to put the html code from a twig template into a html file.
I mean, in this case
$html = $this->render('SiteBundle:Generated:home_news.html.twig', array('news' => $news))->getContent();
Is there a way where I can do this?
$html->output($html_file);
And it gerenates a html file with the template.
I'm doing this because I have news from a wordpress blog and I want to show the latest news in the homepage. So I would want to put the news of my site in a static file to avoid to generate it with each home request.
If you check documentation more carefully, you'd find this chapter, which says:
$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
To complete the #VitalityZurian answer:
First, generate your markup :
$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
Then, write in file :
$file = file_put_contents($filename, $content);
You can use Symfony2 help methods to deal with locations in your bundles:
$bundleResourcesDir = $this->get('kernel')->locateResource('#AcmeAnotherBundle/Resources/views');
file_put_contents($bundleResourcesDir.'/index.html.twig`
And retrieve it:
$view = $this->get('kernel')->locateResource('#AcmeAnotherBundle/Resources/views/index.html.twig');
I am trying to create a back-end interface for silverstripe that gives the CMS user the option to choose between a set of Post Types (like tumblr) in Silverstripe3. So they can choose to create a News Post, Video Post, Gallery Post, etc.
I initially started off giving all Posts the necessary fields for each Type and adding an enum field that allowed the user to choose the Post Type. I then used the forTemplate method to set the template dependent upon which Post Type was chosen.
class Post extends DataObject {
static $db = array(
'Title' => 'Varchar(255),
'Entry' => 'HTMLText',
'Type' => 'enum('Video, Photo, Gallery, Music')
);
static $many_many = array(
'Videos' => 'SiteVideo',
'Photos' => 'SitePhoto,
'Songs' => 'SiteMp3'
);
public function forTemplate() {
switch ($this->Type) {
case 'Video':
return $this->renderWith('VideoPost');
break;
case 'Photo':
return $this->renderWith('ImagePost');
break;
etc...
}
function getCMSFields($params=null) {
$fields = parent::getCMSFields($params);
...
$videosField = new GridField(
'Videos',
'Videos',
$this->Videos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
$photosField = new GridField(
'Photos',
'Photos',
$this->Photos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
return $fields;
}
}
I would rather the user be able to choose the Post Type in the backend and only the appropriate tabs show up. So if you choose Video, only the Video GridField tab would show up. If you choose Photo Type only the Photo's GridField would show.Then I would like to be able to call something like
public function PostList() {
Posts::get()
}
and be able to output all PostTypes sorted by date.
Does anyone know how this might be accomplished? Thanks.
Well the first part can be accomplished using javascript. Check out this tutorial and the docs let me know if you have questions on it.
The second part would be trickier but I think you could do something with the page controller. Include a method that outputs a different template based on the enum value but you would have to set links somewhere.
I managed this with DataObjectManager in 2.4.7 as I had numerous DataObjects and all were included in one page but I'm not sure if that is feasible in SS3.
return $this->renderWith(array('CustomTemplate'));
This line of code will output the page using a different template. You need to include it in a method and then call that method when the appropriate link is clicked.
I am new to Drupal and I am working on creating my own theme for our custom module. Can any one please help me understand how to register our theme functions or share any ideas or links that explains the process from scratch.
Register a theme function means implementing hook_theme in your module.
For example, if your module is called "example", then you need to have a function called example_theme in the example.module file. The theme function must return an array or you'll end up with the famous white screen of death.
In example.module:
<?php
// $Id$
// Implements hook_theme
function example_theme(){
return array(
'mydata' => array(
// Optionally, you can make the theme use a template file:
// this line references the file "mydatafile.tpl.php" in the same folder as the module or in the folder of the active theme
'template' => 'mydatafile',
// these variables will appear in the template as $var1 and $var2
'arguments' => array(
'var1' => null,
'var2' => null,
),
),
'myotherdata' => array(
// these variables will appear in the functions as the first and second arguments
'arguments' => array(
'var1' => null,
'var2' => null,
),
)
);
}
// If you don't want to use a template file, use a function called "theme_THEID" to create the HTML.
function theme_myotherdata($var1, $var2){
return "<div>var1= $var1 and var2= $var2</div>";
}
In mydatafile.tpl.php:
<div>mydatafile.tpl.php was called</div>
<ol>
<li>var1: <?php echo $var1; ?></li>
<li>var2: <?php echo $var2; ?></li>
</ol>
You can then later call the theme function manually if needed:
$html = theme('mydata', 'hello world', 123);
$html = theme('myotherdata', 'hello world', 123);
In which case "mydatafile.tpl.php" and "theme_myotherdata" will receive the value "hello world" in $var1 and the value 123 in $var2.
There are many more options, like changing the name of the function, using patterns instead of a fixed name, being able to have the function in another php file or such, check out the link.
Here are a couple more ressources about theming:
The theme guide
Overriding themable output
The Devel module
The Theme developer module (requires the Devel module)
a longuer example that also creates a form/page to use the theme function
By the way, you will need to rebuild the theme registry cache if you add the functions in the .module file after it has been installed, in which case you can do so by clearing the cache (one way is using the button at the bottom of the the Performances page).
NOTE: Slightly different syntax with Drupal 7 (ex. 'arguments' changes to 'variables')
http://www.davidcalculli.com/blog/2012/01/drupal-7-custom-template-only-displaying-the-first-character