symfony2 generate static html with twig - symfony

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');

Related

Drupal: Password textfield in everypage, how can I remove?

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

Silverstripe date and image error

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.

Customize drupal search result page

I have created a customized theme called "test", and I am trying to display the search results.
I added the function test_preprocess_search_results() to the template.php (copied the code from drupal page "function template_preprocess_search_results", then copy the search-result.tpl.php from search module to the "test"'s template folder.
function test_preprocess_search_results(&$variables) {
$variables['search_results'] = '';
if (!empty($variables['module'])) {
$variables['module'] = check_plain($variables['module']);
}
foreach ($variables['results'] as $result) {
$variables['search_results'] .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
}
$variables['pager'] = theme('pager', array('tags' => NULL));
$variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];
}
I am new in drupal, my concern is how can I make the search result display in some certain div which claimed in my page.tpl.php file? Do something like <?php print render($page['search_result']); ?> in page.tpl.php div ? I am not sure how does the default theme know where to display the search result? Can anyone help thanks
ps: after what I did, and refresh the cache, nothing show up
For search results you should use the search-results.tpl.php (all results list) and search-result.tpl.php (single list item) templates. Copy them from ROOT/modules/search folder into your theme folder and override them as you prefer.

Wordpress rss feed as post type with image/permalink

I have a friend who writes to a couple of different blogs and wants to pull stories from one that isn't so popular, onto his main blog with an rss feed and display an image from it too (since rss feeds have images, sometimes).
Displaying the rss feeds shouldn't be too hard, it's making them a custom post type that seems more difficult to me.
If anyone has any ideas, shoot.
EDIT:-
Does anyone know how to get an external rss feed to appear as a custom post type in wordpress?
A simple way may be to use Wordpress' own fetch_feed function:
http://codex.wordpress.org/Function_Reference/fetch_feed
A quick example (assuming you've already set up your custom post type):
function import_feed_items()
{
$feed = fetch_feed('http://feeds.bbci.co.uk/news/uk/rss.xml');
if( !is_wp_error($feed) )
{
if( $last_import = get_option('last_import') )
{
$last_import_time = $last_import;
} else {
$last_import_time = false;
}
$items = $feed->get_items();
$latest_item_time = false;
foreach ( $items as $item )
{
$item_date = $item->get_date('Y-m-d H:i:s');
if( $last_import_time && ($last_import_time >= strtotime($item_date)) )
{
continue;
}
$post = array(
'post_content' => $item->get_content(),
'post_date' => $item_date,
'post_title' => $item->get_title(),
'post_status' => 'publish',
'post_type' => 'custom_post_type'
);
wp_insert_post($post);
if( strtotime($item_date) > $latest_item_time )
{
$latest_item_time = strtotime($item_date);
}
}
if( false !== $latest_item_time )
{
update_option('last_import', $latest_item_time);
}
}
else
{
echo $feed->get_error_message();
}
}
add_action('wp', 'import_feed_items');
If there is an image tag in the content you could use php's DomDocument class to grab the url and upload it to your server so you can set it as the featured image.
http://codex.wordpress.org/Function_Reference/wp_insert_attachment
http://codex.wordpress.org/Function_Reference/set_post_thumbnail
Edit
corrected the timestamp check. This updated example uses the 'wp' hook to run so you can see the results quicker. It would be preferable to set this as a cron task. See http://codex.wordpress.org/Function_Reference/wp_schedule_event
The Feed to Post plugin is the perfect solution for importing multiple RSS items into your WordPress blog, you can even store them as posts or custom post types.
http://www.wprssaggregator.com/extension/feed-to-post/
Unfortunately I don't know a way off hand, but have you looked at modifying a plugin? There are tons of content curation plugins around (feedwordpress, autoblog, etc.). You could probably find the wp_insert_post() line somewhere and modify it to include your custom post type/taxonomies.
EDIT
Jumped into the plugin (feedwordpress) myself, and all the insert_post stuff is in syndicatedpost.class.php - the main wp_insert_post() on line 1538
EDIT if you fancy a giggle while reading throught that plugin code, you'll find many an instance of the f word... haha
Hey why dont you try this http://wordpress.org/extend/plugins/display-latest-rss-feeds/
It would pull rss feeds from any account and display it to your blog. Unfortunately it wont display images just the rss feeds title and its permalink to the original blog but you can easily modify the source code if you want.

Drupal 7: how to add template file to /node/add/content-type

In Drupal 7.
I want to themming /node/add/content-type by template file as page--node--add--content-type.tpl.php.
Please help me.
You need to use
page--node--add--content_type.tpl.php
instead of
page--node--add--content-type.tpl.php
underscore(_) instead of dash(-)
You will need to add the template as a suggestion in a preprocess function or you can create a custom theme function for the form, that will use the desired template. This is much similar to how it was done in Drupal 6, only you can do form_alters in your theme now.
you can add this function to your template.php:
you can print_r() your form and see what are the fields.
function yourThemeName_form_yourFormID_alter(&$form, &$form_state, $form_id) {
// Modification for the form with the given form ID goes here. For example, if
// FORM_ID is "user_register_form" this code would run only on the user
// registration form.
print_r($form);
}
You are adding tpl file for node form. For this you have to define tpl file name in template.php under your theme.
First create your_theme_name_theme()
Print content on tpl form.
Example: In below example contact_site_form is the form id of content type.
your_theme_name_theme() {
$items['contact_site_form'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'your_theme_name') . '/templates',
'template' => 'contact-site-form',
);
}
now create a file contact-site-form.tpl.php in your path location as specify above.
<?php echo render($form['name']); ?>
<?php echo render($form['mail']); ?>
.
.
.
<?php print drupal_render_children($form); ?>
You can render each element separately in given method.

Resources