Does anyone know how to add stylesheets in a template with Symfony 1.4 ?
I have tried everything I can think of, from modifying frontend/config/view.yml to modifying the template itself - bothing works.
I have seen from my searches, that other people have had the same problem. There seems to be a clash of sorts between using include_stylesheets and use_stylesheets - however this is not documented anywhere AFAIK.
Edit:
Ok I think I got it now. You should add include_stylesheets() into the head section of your layout file:
<html>
<head>
<title>This is the title</title>
<?php include_stylesheets() ?>
</head>
<body>
<!-- ... -->
Then in your template file, you use use_stylesheet() to add a particular stylesheet for this template:
<?php use_stylesheet('/path/to/stylesheet.css') ?>
From the API documentation:
include_stylesheets()
Prints <link> tags for all stylesheets configured in view.yml or added to the response object.
use_stylesheet()
Adds a stylesheet to the response object.
Same for Javascript.
According to the API documentation it should still work in 1.4, sfWebResponse still has this method:
addStylesheet ($file, $position, $options)
$file The stylesheet file
$position Position
$options Stylesheet options
Adds a stylesheet to the current web response.
At least the method exists.
What exactly is the problem? Do you get an error if you want to call that method or is the stylesheet just not added?
http://www.symfony-project.org/tutorial/1_4/en/upgrade#removal_of_the_common_filter
As of 1.4 your javascripts and stylesheets are no longer automatically injected into your head tag. Instead, you need to include the following in your layout where you'd like them to be placed:
<?php include_javascripts() ?>
<?php include_stylesheets() ?>
and just in case your post title wasn't a typo you'll want to use addStylesheet('...') off of the response:
$sf_response->addStylesheet('main');
$sf_context->getResponse()->addStylesheet('style.css')
$sf_context->getResponse()->addJavascript('script.js')
Related
I have few questions on partials and overriding templates.
For that i used the following folder structure.
projectRoot
dust-core-0.6.0.min.js
jquery.js
test.html
partial.tl
main_without_override.tl
The content of partial.tl:
{+greeting} Hola {/greeting}
{+world} World {/world}
The content of main_without_override.tl:
{>partial/}
The content of test.html:
<!DOCTYPE html>
<html>
<head>
<script src="dust-core-0.6.0.min.js" type="text/javascript"></script>
<script src="jq.js" type="text/javascript"></script>
</head>
<body>
</body>
<script>
$.get('main_without_override.tl', function(){
console.log(arguments);
})
</script>
</html>
In the index.html when i try to get the main_without_override.tl its saying 404. But im sure that the file is there. The path that firebug is showing is correct.But browser says 404.
I want to know
How to get this main_without_override.tl
Apply templating for main_without_override.tl and render in the browser.
I searched in google most of the examples give only the syntax. Can somebody help me in rendering the main_without_override.tl template.
In order to compile templates on the client (which is probably not a really good idea), you need to include dust-full instead of dust-core. This is because dust-core does not include the Dust compiler.
The reason that compiling templates on the client is probably not a good idea is that Dust compiles to JavaScript and as #monshi mentioned, you can compile the templates and then serve them as JavaScript. It is possible to get .tl files through AJAX if you include dust-full, but it is a better idea to compile that template beforehand and then make a dynamic request for that .js file when you need.
You can include your dust template as a JavaScript file by using <script> tag, but you need to compile it first, which is explained here
Then add following templates (scripts) to test.html:
<script type="text/javascript" src="partial.js"></script>
<script type="text/javascript" src="main_without_override.js"></script>
And in you JavaScript render the template by dust.render method:
dust.render('main_without_override', your_json_object, function(err, out){
your_dom_element.innerHTML = out;
});
Related question:
how to use dustjs-linkedin as client side templating?
Am trying to implement internationalization support to my project for this people suggested google Closure Templates.but am very new to closure templates.am trying to get the language specific messages using closure template but am not getting in xlf file.If any one knows how to generate language specific messages using closure template, please tell me the steps.that's great help to me.
My .soy file code as bellow.
{namespace poc}
/**
*Testing message translation
*#param pageTitle
*/
{template .translate}
<HTML>
<Head>
<title>{$pageTitle}
</title>
</head>
<div>
{msg desc="Hello"}Hello{/msg}
</div>
</html>
{/template}
and generated .xlf content as bellow
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file original="SoyMsgBundle" datatype="x-soy-msg-bundle" xml:space="preserve" source-language="en" target-language="pt-BR">
<body>
<trans-unit id="2286494898080570401" datatype="html">
<source>Thanks</source>
<target/>
<note priority="1" from="description">Says thanks</note>
</trans-unit>
</body>
</file>
</xliff>
I see you already used the SoyMsgExtractor to create the base xlf. Next you need to make translations of this base xlf to the languages you want to support. A file for each language is created. I used the xliff exitor from Translution. http://sourceforge.net/projects/eviltrans.
Next, using the SoyToJsSrcCompiler a translation soy can be made per language:
java -jar SoyToJsSrcCompiler.jar --shouldGenerateGoogMsgDefs --bidiGlobalDir 1 --messageFilePathFormat Filename_en-us.xliff --outputPathFormat FileName_fr.js *.soy
This will create a Filename._fr.js file that contains the compiled soy file.
Including this file instead of the original soy (or compiled) will create a localized version.
Good luck!
\Rene
i think the easiest way is to make (i.e. generate from whatever source) a separate js file which contains one messages object and reference it through an extern declared function.
it justs works and has no complicated dependencies.
I want to use jQueryUI Autocomplete on my Drupal site and I downloaded the .js files that are needed for it to function. Now after that, I saved my .js files on my themes folder located at /sites/all/themes/advanced/js. And in my page.tpl.php there's this code,
<head>
<?php print $head ?>
<title><?php print $head_title ?></title>
<?php print $styles ?>
<?php print $scripts ?>
<?php print phptemplate_get_scripts(); ?>
<?php if ($user->uid) print phptemplate_get_scripts_advanced(); ?>
<!--[if lt IE 7]>
<?php print phptemplate_get_ie_styles(); ?>
<![endif]-->
</head>
with the code above I'm assuming that my .js files will be included but when I reloaded the page and check the running scripts through Firebug, I could not find them. What did I miss? Thanks in advance.
Got it. I did try to put my scripts manually on my template.php and that solved it. :)
I decided to use jQuery UI for my autocomplete opposed to a plugin because I read that the plugins are deprecated. My overall goal is to have an autocomplete search bar that hits my database and returns users suggestions of city/state or zipcodes in a fashion similar to google. As of now I am not even sure that the .autocomplete function is being called. I scratched everything I had and decided to start with the basics. I downloaded the most recent version of jQuery UI from http://jqueryui.com/download and am trying to get the example that they use here http://jqueryui.com/demos/autocomplete/ to work. All the scripts that I have included seem to be connected at least linked through Dreamworks so I am fairly certain that the paths I have included are correct. The CSS and Javascripts that I have included are unaltered straight from the download. Below is my HTML code and my backend PHP code that is returning JSon formated data. Please help me. Maybe I need to include a function that deals with the JSon returned data but I am trying to follow the example although I see that they used a local array.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQueryUI Demo</title>
<link rel="stylesheet" href="css/ui-lightness/jquery-ui-1.8.17.custom.css" type="text/css" />
<script type="text/javascript" src ="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src ="js/jquery-ui-1.8.17.custom.min.js"></script>
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#tags").autocomplete({
source: "search_me.php"
});
});
</script>
</head>
<body>
<div class="demo">
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>
</div><!-- End demo -->
<div class="demo-description">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
<p>The datasource is a simple JavaScript array, provided to the widget using the source-option.</p>
</div><!-- End demo-description -->
</body>
</html>
Below the PHP part.
<?php
include 'fh.inc.db.php';
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));
$location = htmlspecialchars(trim($_GET['term'])); //gets the location of the search
$return_arr = array();
if(is_numeric($location)) {
$query = "SELECT
zipcode_id
FROM
user_zipcode
WHERE
zipcode_id REGEXP '^$location'
ORDER BY zipcode_id DESC LIMIT 10";
$result = mysql_query($query, $db) or die(mysql_error($db));
while($row = mysql_fetch_assoc($result)) {
extract($row);
$row_array['zipcode_id'] = $zipcode_id;
array_push($return_arr, $row_array);
}
}
mysql_close($db);
echo json_encode($return_arr);
?>
Thanks for the ideas. Here is an update.
I checked the xhr using firebug and made sure that it is responding thanks for that tip. also the above php code I hadn't initialized $return_arr so i took care of that. Also thanks for the clarification of the js required or rather not required. Now when I type in a zipcode a little box about a centimeter shows up underneath it but I can't see if anything is in there, I would guess not. I went to my php page and set it up to manually set the variable to "9408" and loaded the php page directly through my browser to see what it returned. This is what it returned.
[{"zipcode_id":"94089"},{"zipcode_id":"94088"},{"zipcode_id":"94087"},{"zipcode_id":"94086"},{"zipcode_id":"94085"},{"zipcode_id":"94083"},{"zipcode_id":"94080"}]
I then went to a JSON code validator at this url http://jsonformatter.curiousconcept.com/ at it informed me that my code is in fact returning JSON formatted data. Anymore suggestions to help me troubleshoot the problem would be terrific.
Wow after more research I stumbled across the answer on someone another post.
jquery autocomplete not working with JSON data
Pretty much the JSON returned data must contain Label or Value or both. Switched the zipcode_id to value in my $row_array and... boom goes the dynamite!
Your scripts (js files) references are not correct, should only be:
<!-- the jquery library -->
<script type="text/javascript" src ="js/jquery-1.7.1.min.js"></script>
<!-- the full compressed and minified jquery UI library -->
<script type="text/javascript" src ="js/jquery-ui-1.8.17.custom.min.js"></script>
The files "jquery.ui.core.js", "jquery.ui.widget.js" and "jquery.ui.position.js" are the separated development files, the jquery ui library is splitted into modules.
The file "jquery-ui-1.8.17.custom.min.js" contains them all, compressed and minified !
Concerning the data source, as stated in the "Overview" section of the Autocomplete documentation: when using a an URL, it must return json data, either of the form of:
an simple array of strings: ['string1', 'string2', ...]
or an array of objects with label (and a value - optionnal) property [{ label: "My Value 1", Value: "AA" }, ...]
I'm really not familiar with PHP so just make sure your php script returns one of those :-)
I have 2 tables, page and settings.
page is just a bunch of fields, such as name and slug, and has 3 other fields for meta tags (title, keywords, description) and displays a cms page.
The settings has 3 fields: default_meta_title, default_meta_keywords, default_meta_description
Now what I'm looking to do is to display the default_meta_* tags in the HTML source if the page I am on does not have the particular meta info set from the cms page.
All pages, except the homepage is managed this way, so I was thinking I'd need to add some code to the layout.php to get this to work.
So the homepage will display my default_meta_*, as I cannot set this in the cms pages table.
There are two ways to solve the problem.
First is to use sfYaml class to update view.yml with default meta tags (see documentation about view.yml). After that if specific page should use another metas you can override defaults with addMeta method of response object
Second (as ManseUK suggested) is to declare slot placing code like this into layout
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<?php include_javascripts() ?>
<?php include_stylesheets() ?>
<?php include_title() ?>
<?php if (has_slot('metas')): ?>
<?php include_slot('metas') ?>
<?php else: ?>
<?php include_component('page', 'metas') ?>
<?php endif; ?>
<link rel="shortcut icon" href="/favicon.ico" />
</head>
<body>
Default metas will be rendered via page components. On top of your template (i guess modules/page/templates/showSuccess.php) place code
<?php slot('metas') ?>
<?php if($page->hasMetas()):?>
<!-- code to render nondefault page metas -->
<?php echo $page->getMetas(); ?>
<?php else: ?>
<?php include_component('page', 'metas') ?>
<?php endif;?>
<?php end_slot() ?>
I assume that you will replace $page->hasMetas() with real code that will check if your page object has metatags.
Actually i would prefer to go further and code page components to accept parameters. Code in a template will look like
<?php slot('metas') ?>
<?php include_component('page', 'metas', array('metas'=>$page->getMetas())) ?>
<?php end_slot() ?>
Deciding which metas (default or not) should be rendered will take place in page components (i assume that you can easily retrieve defaul;t settings from your database). If no parameters were passed (see layout code) than your component should also render default metas.
I hope this will help.
You could use a slot - check for existence of the slot in the layout - if it exists then add the custom meta fields - if not add the default ones