VBscript pass parameters to included file - asp-classic

Ok, I'm new to the asp/vbscript world. I am working at a new company and am trying to reproduce a script I use on almost all projects when developing under php.I have two functions one called showHeader and one called showFooter. These functions both get arguments passed to them and those arguments need to be displayed in the included file. For example in php my showHeader function is like so
<?php
showHeader($page,$title,$passedCSS,$desc,$keywords) {
include("header.php");
}
?>
Now in the include file i can echo out the contents of any of those arguments simply by calling echo $var and i get the contents. Is this possible with vbscript. I'm having no luck what so ever.

#projectxmatt: You could do something like --
In header.asp:
<%
Sub showHeader(page, title, passedCSS, desc, keywords)
%>
<!-- some HTML code here -->
<title><%=page %></title>
<!-- more HTML code here -->
<%
End Sub
%>
In somefile.asp:
<!-- #include file="header.asp" -->
<% showHeader "value-for-page", "My Page Title", "", "", "" %>
With ASP you have to specify all the variables you have in your Sub or Function, they can't be left out or assigned with a default as in PHP if none was passed to the function (e.g. function showHeader(title = 'Default Value'))

Ok here is what i did
global.asp
<%#LANGUAGE="VBSCRIPT"%>
<%
Sub showHeader(page,title,passedCSS,desc,keywords)
%>
<!---#include file="header.asp"--->
<%
end Sub
Sub showFooter(passedJS)
%>
<!---#include file="footer.asp"--->
<%
end Sub
%>
Then in header.asp and footer.asp I passed in the vars just using <%=varname%>
Then in the main.asp my code was as follows.
<!---#include file="global.asp"--->
<% showHeader "Home","Test Page","test.css","Description","keywords" %>
<section>
</section>
<aside>
</aside>
<section>
<h2></h2>
<ul>
<li><article></article></li>
<li><article></article></li>
</ul>
</section>
<section>
<div></div>
<div></div>
<div></div>
</section>
<% showFooter "testjs.js" %>
And everything works great.

Response.Write would be another solution you could use to write output in classic ASP.

Related

ASP include file into variable

I have a template.aspx file the contains code like this:
<html>
....
<body>
<div id="wrapper">
<div id="container">
----sideMenu---
.........
<div id="body">
<%=pageContent%>
</div>
</div>
</body>
</html>
I have a file index.aspx
with the code:
<%
Dim pageContent = ""
%>
<!--#include virtual="template.aspx"-->
I want to insert in pageContent a lot of html code and asp code
without connecting strings like this:
pageContent = "<div class..." &_
" bla bla bla " & aspVariable &_
"more divs and html and asp variables"
.
.
.
actually i want a dyanmic pages i dont want write the template html codes each pages i want that only the pageContent are change.
It would be more common to create a handful of template files to be included in each page of your ASP app. Such as:
header.inc (might contain site, banner... top of page stuff)
topNav.inc (might contain code to generate the top navigation bar)
leftNav.inc (take a guess ;))
bottomCopy.inc (might have text links as site map and copyright statement.
Then, in each page, include all where they should live. For example, in my page called "theFirst.aspx", I might have:
<!-- #include file = "cacheprevent.inc" -->
<!-- #include file = "accesscheck.inc" -->
<!-- #include file = "header.inc" -->
<!-- #include file = "topNav.inc" -->
<!-- #include file = "leftNav.inc" -->
<%
' Here goes the rest of your code to generate content for this specific page
%>
<!-- #include file = "bottomCopy.inc" -->
All this assumes it is in ASP-Classic, as you've tagged your post, at least in part.
... and you could in some cases, combine the top four... if all pages would have the same layout regions.

Pass Variable from Content page to Master Page in classic ASP

I am new to classic ASP and I am trying to create a Master Page with variable placeholders and fill the information on that page with variables that are worked on a content page.
my master page looks like this:
<html>
<head>
<title>Template Loaded Properly</title>
</head>
<body>
<% call BodyContent %>
<span>Title: <% //place title here %></span>
<span>Content: <% //place content here %></span>
</body>
</html>
and the content page like this:
<!--#include virtual="/templates/TEMPLATE.html" -->
<% sub BodyContent %>
var Title = "This is the title"
var Content = "Here goes some content"
<% end sub %>
Any help will be appreciated.
Once you include the page with the variables, you can treat them as if they were created right then and there (because in a sense they are created right then and there, at least from the server's point of view). You do need to make the variables global in scope [read: dim them outside the sub], unless you want to list all of them when calling your BodyContent sub. (Personally, I don't see the point, but some people are unreasonably allergic to global variables.)
<%
dim Title, Content
sub BodyContent
Title = "This is the title"
Content = "Here goes some content"
end sub
%>
<body>
<% call BodyContent %>
<span>Title: <%=Title%></span>
<span>Content: <%=Content%></span>
</body>
One caveat, though: include files are processed long before the code, so you can't vary what file is included. In other words, don't do this:
<%If x = a Then%>
<!-- #include virtual="/templateA.inc" -->
<%Else%>
<!-- #include virtual="/templateB.inc" -->
<%End If%>
The result of trying something like that is that both templateA and templateB will be included. If you need conditional includes, look into using FileSystemObject to read the content of the appropriate template, and then using Execute to, well, execute it.

change frames/iframes to just one page #aspclassic

How do I rewrite the following PHP code in Classic ASP?
<head></head>
<body>
<h1>Test</h1>
<section><?php echo include('content/'.$_GET['p'].'.php') ?></section></body>
If the url is http://foo.bar.com/admin.php?p=pages, then content/pages.php is shown.
You can't do this with INCLUDE but you could use Server.Transfer instead, eg.
Server.Transfer "content/" & Request.Form("p") & ".asp"
Never trust what comes from the clientside (browser) as they can try to hack you.
Since it is not likely that "shdyhio3hlkehio.asp" or similar is a proper file, you should limit the options to your actual selection and also have a default file which is a "catch all other requests".
Combine that with Server Side Includes and you have your setup ready.
You should also check if the user actually requested a page -- if "p" is empty then show a default message.
Note the use of LCase in the "Select Case"-line and lowercase values in the Case-lines.
This is due to the face that the string comparison is case-sensitive, meaning "about" (lowercase "a") and "About" (uppercase "A") is not the same.
Eksemple:
<% If Request.QueryString("p") = "" Then %>
no specific page was request, show a default message
<% Else %>
<% Select Case LCase(Request.QueryString("p")) %>
<% Case "content" %><!-- #include file="content.asp" -->
<% Case "about" %><!-- #include file="about.asp" -->
<% Case "contact" %><!-- #include file="contact.asp" -->
<% Case Else %><!-- #include file="404.asp" -->
<% End Select %>
<% End If %>
You can use something like this
<!--#include file="somefile.asp"-->
in your HTML file .

If / Else condition with OR clause not working in Classic ASP

I am checking if condition in Classic ASP but its not working properly. Here is the code that isn't working:
<% If (Request.QueryString("Cat")="") Then %>
<a class="home selected" href="/" alt="Home"></a>
<%Else%>
<a class="home" href="/" alt="Home"></a>
<%End If%>
This displays both anchor tags but I want to display only one from both.
Please give me suggestions on how to fix it.
Sean has taken is a step in the right direction but but this is some include asp isn't it, providing some kind of common navigation bar. Consider this approach.
<%
''# Some where at the top of the include we have a set of utlity functions
Function GetSelectedClass(cat)
If (Request.QueryString("Cat") = cat) Then
GetSelectedClass = " selected"
End If
End Function
''# Other utility functions here
%>
<!-- Here we start the common navigation HTML -->
...
<a class="home <%=GetSelectedClass("")%>" href="/" alt="Home"></a>
<a class="tables <%=GetSelectedClass("tables")%>" href="/List.asp?Cat=tables" alt="Tables"></a>
<a class="chairs <%=GetSelectedClass("chairs")%>" href="/List.asp?Cat=chairs" alt="Chairs"></a>
While I don't see any issue with the code that is posted, I cleaner way of doing this:
<%
Dim Cat, Selected
Cat = Request.QueryString("Cat")
If (Cat = "") Then
Selected = " selected"
End If
Response.Write("<a class=""home" & Selected & """ href=""/"" alt=""Home""></a>")
%>

Classic asp include

I am trying to separate some asp logic out into a separate page.
For now, I am trying to call a simple function.
Here is the simple index page that I am using
<html>
<head>
<title>Calling a webservice from classic ASP</title>
</head>
<body>
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
%>
<!--#include file="aspFunctions.asp"-->
<%
doStuff()
End If
%>
<FORM method=POST name="form1" ID="Form1">
ID:
<INPUT type="text" name="corpId" ID="id" value="050893">
<BR><BR>
<INPUT type="submit" value="GO" name="submit1" ID="Submit1" >
</form>
</body>
</html>
Here is aspfunctions.asp
sub doStuff()
Response.Write("In Do Stuff")
end sub
When i hit the submit button on my form i get the below
sub doStuff() Response.Write("In Do Stuff") end sub
Microsoft VBScript runtime error '800a000d'
Does anyone have any idea what i could be doing wrong?
Any help is greatly appreciated
Thanks
Damien
Type mismatch: 'doStuff'
/uat/damien/index.asp, line 15
You must have the asp functions inside the <% %> tag.
aspfunctions.asp should be inside tags so the asp is "executed", e.g.
aspfunctions.asp file:
<%
sub doStuff()
Response.Write("In Do Stuff")
end sub
%>
Otherwise the asp in aspfunctions.asp is just seen as plain-text, so as far as the server is concerned, doStuff has never been defined.
You're including the other file within an if statement. This does not mean that it's dynamically included, it's not. It will always be included.
To see this in action try this sample:
<%
If 1=0 Then
'We never get here
%>
<!--#include file="aspFunctions.asp"-->
<%
dostuff()
End If
dostuff()
%>
If I remember correctly, you need no brackets for calls without a return value (untested solution):
doStuff
Make changes in two places:
In aspfunctions.asp write "sub doStuff" instead of sub doStuff()
Call the function as doStuff not doStuff()

Resources