I want to have a functionality in Magento where i can add text field to the admin are and allow a user to add a code snippet which will have a placeholder for a variable in it.
At runtime I will grab the code snippet via getStoreConfig and replace the variable/placeholder with the value i need.
I have managed to do everything apart from how to add the variable and replace.
So i do not need help in creating the config in admin etc.
An example would be that the user entered in a config field the following:
<script>
alert('{{amazing_value}}');
</script>
then in my template i would do:
$codeSnippet = Mage::getStoreConfig('path_to_config');
$amazingValue = "This will replace the variable";
// Something here :) (this bit im not sure about where the variable is translated)
echo $codeSnippet
The output from then be:
<script>
alert('This will replace the variable');
</script>
In your "Something here" area, you could try:
$codeSnippet = str_replace("{{amazing_value}}",$amazingValue,$codeSnippet);
Related
I Would like to know if there is a possibility to modify an external js file via ajax post, for example:
Into my external js file i've got a variable :
var color;
So i would like my users to be able change the value of this variable by typing the HEX code into an input text form.
So when the type and press submit button to grab this value and post it to external js file and modify the variable.
I want something like this:
var colorVal = $('input').val();
$.post("external-file-js.js", {color: colorVal}, function(result){});
In external js file something like:
var color = $.get(colorVal); // HERE i dont know how to grab the value
$('body').css('background-color',color);
Thank you :)
I need to understand the use case you are intending in order to provide a full answer. If all you are attempting to do is change the background color, why do you need to run an AJAX post at all? Why not just change it?
In extenal.js (which is included in html body):
function changeColor(color) {
$('body').css('background-color',color);
}
Then you bind the following event to the input:
$('input').change(function () {
// Though you may want to perform validation first.
changeColor($(this).val());
});
The only problem is if you need to change it long term, for multiple users. Then you would need to store the value server side (with a post and some type of CRUD system, in which case, check out JSON/JSONP)
It can be done. You would have to use some back-end code to rewrite your JS file. You would then need to remove any binds and use a script to reload your js document on the fly. Here is an example of loading JS on the fly. http://www.philnicholas.com/2009/05/11/reloading-your-javascript-without-reloading-your-page/
I am not sure why you would do this. I would just rework my JS file so I can avoid this mess.
I have a basic page: base.tpl which contains the header and footer of every page on my site. Specific pages (Example: home.tpl) extends ({extends file='../templates/base.tpl'}) the basepage. In the base page I have an include for a statusbar, right underneath the header
({include file=$statusbar}). I include the right statusbar depending if the user is logged in or logged out... But in some occasions, I don't wish to include the statusbar. When the user chose to register, I just want to show the register form, or when a user has tried to loggin and fail I wish to show him a detailed page, with extra info on how to register/retrieve password.
But if I try to include an empty value ($smarty->assign("statusbar","")) I get an error and nothing on the page renders. I was considering adding a blank .tpl and link to it. But I was wondering if that is a "good" solution. Is there a better way to do it?
You can define blocks for you template sections. Then on your extending templates leave blank that blocks
ex: base.tpl
<head>{block name = "head"}<title>Welcome</title>{/block}</head>
{block name = "status_bar"}{include file = $statusbar}{/block}
{block name = "footer"}Some footer{/block}
ex :register.tpl
{extends file = 'base.tpl'}
{block name = 'head'}<title>Register</title>{/block}
{block name = 'status_bar'}{/block}
If you do not to want change the content of any block just dont type
Yes, you just have to add a conditional to see if $statusbar has any value:
{if $statusbar!=''}
{include file=$statusbar}
{/if}
By default parent::display($tpl); loads components/com_my_component/views/my_component/tmpl/default.php, but in some cases i need to load other php file which is in the same folder near default.php (for example components/com_my_component/views/my_component/tmpl/lol.php). How to do this from view.html.php.
P.S.
Tried load loadTemplate and setLayout methods with no luck.
Solved the problem by myself. Need to use the method setLayout and pay attention to the input syntax
$this->setLayout('dafault:lol');
parent::display($tpl);
By default, joomla looks for the layout keyword in the URL to decide which layout to display. If this variable is empty or not present then the tmpl/default.php layout will be loaded.
By editting your view.html.php file you can set the default layout by using the JView API, e.g. $this->setLayout('lol') will make the URL example.com/yourview equivalent to example.com/yourview?layout=lol.
However, this change alone will result in Joomla overriding it's default behaviour so that the layout request will be ignored. This means that the request example.com/yourview?layout=lmao will also display example.com/yourview = example.com/yourview?layout=lol
You can solve this easily by adding a condition around the setLayout function so that only if the layout keyword is not present then you will set the default layout to lol, e.g.
<?php
# ...
function display($tpl = null) {
# ...
# Edit : Set the default layout to 'lol'
$layout = JRequest::getWord('layout', '');
if (empty($layout)) $this->setLayout("lol");
// Display the view
parent::display($tpl);
}
# ...
I keep coming back to this and I've yet to find a satisfying solution.
What does work, from J1.5 right up to J3.4, for me has always been to set the $tpl variable in view.html.php
If $tpl is empty or "" then tmpl/default.php is displayed by default.
If you change $tpl to a string, e.g. $tpl="stacker" then it will look for and display tmpl/default_stacker.php
I've seen various differing theories on changing it earlier in the MVC so that it doesn't need the default_ pretext. e.g. tmpl/stacker.php
None have worked for me.
I want to avoid script alert in my view page.That is when i users add script in text box or test area ,this script should display as script in my site's view page.
For example,
If user enter a message in text area like alert('hai');, this should display as alert('hai');. But now its alert hai in alert box, when the view page is opened .
To stop this from happening use the native php function 'strip_tags'
$stripped = strip_tags($content);
Do this before you echo the data from the database.
Alternatively, if you want the tag to remain but without it being parsed use something like str_replace or preg_replace:
$stripped = str_replace("<", "<", $content);
Or better yet: htmlspecialchars()
etc.
I am working on website (built using PHP, Mysql, jQuery) which require that admin set a variable in configuration and according to that configuration variable jQuery autocomplete is enabled or disabled to all website. Is there any way to achieve that functionality.
The easiest way to do it is to add a common id, or other attribute to all the parent elements where the schedule shows up. Then include into your main javascript file something of the form
FLAG = 1; //for visibile, or 0 for invisible
$('#calendar').toggle(FLAG); //OR
$('[rel=calendar]').toggle(FLAG);
If you wanted that to come from php, you can always load your javascript within php:
functions.js.php:
FLAG = <?php $CAL_SETTING ?>
//go on with the rest of the javascript
//then include it into your html
<script language="javascript" src="functions.js.php"></script>
Good luck!
create a table setting with these fields [name, value,..] and set the name calendar_status value hide
in your view you can get read of that field from you DB and set the myCalendar.hide(true); or myCalendar.hide(false);
myCalendar = new dhtmlXCalendarObject("DateStart");
myCalendar.hide(); <- you php code like <?php echo calendar_status; ?>