Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using template variables with javascript

Status
Not open for further replies.

timfoster

Programmer
Dec 19, 2002
110
GB
Hi,

I need to pass a Dreamweaver template variable to some javascript code within the template. The javascript is very straightforward in that it simply sets a form variable to the value held in the template.

The variable is defined as RestrictSearch and the page holds a value of "SCAN" for this instance.

Inside an editable region of the page is a form that comes from an include file.

The template has a region that writes the following javascript from within the template to update this form.

<!-- TemplateBeginIf cond="_document['RestrictSearch']" -->
<script language="javascript">
document.with_restrict.restrict.value='<!-- TemplateExpr expr="RestrictSearch" -->';
</script>
<!-- TemplateEndIf -->

If the page has a value for RestrictSearch (rather than "") the region is written from the template this prevents me getting a script error when the form isn't present.

Ultimately I want to push this javascript code out to an SSI so that it ties with the rest of our template(s) but that's another question.

At present the code works in that it writes the javascript to the page correctly, however the value that is written out is not the value of RestrictSearch (SCAN), rather it's <!-- TemplateExpr expr="RestrictSearch" --> as a string.

Any help would be greatly appreciated.
 
The variable is defined as RestrictSearch and the page holds a value of "SCAN" for this instance.
What does the value actually look like in the generated html? That is what the JavaScript will be seeing. Your code is assigning a defined string to the variable.

Can't you just use a template to assign a value directly to the form? If not, use a template to populate a named hidden text box and read the value from there.

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Remember that your template variable is only a Dreamweaver variable. It's just a string to JavaScript and a comment to html.

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Hi Greg,

Thanks for the comments.

In the generated html the value is the name of the Dreamweaver variable <!-- TemplateExpr expr="RestrictSearch" -->.

It did at, one point, give me a value of SCAN but not as a string 'SCAN'. The problem seems to be that the template code is interpreting it as a string to be passed 'as is' to the javascript.

I can't use the template to pass the value directly as the form sits inside a user editable region. The form itself is inside an SSI and is used by a number of different pages.

I'm going to have a think about your idea of having a hidden form but I'm not sure that's going to give me any advantage over using the additional SSI to hard code the value into a line of javascript.
 
Forget my earlier comments - hadn't had enough caffeine yet.[blush] Have you tried the @@(RestrictSearch)@@ format to assign variables?

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Yup! Tried that first. If I put it into the bottom of the template (so that it just writes it out to the page) it works fine, regardless of which format I use. It's when I try passing it to a javascript function that I seem to get a problem.
 
Have you looked up this problem in the Adobe helpdesk? I'm pretty sure if you go searching, you will find an explanation along the lines of .dwt and ssi are not compatible. It seems that DW engineers decided that templates were for small websites and idiots like me who need a simple way to keep track of a few pages. But larger sites which typically use ssi would use that feature and not need templates.

Try detaching your page from the template ..... ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top