Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old May 13th, 2007, 07:05 AM
KiReSt KiReSt is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 116 KiReSt User rank is Sergeant (500 - 2000 Reputation Level)KiReSt User rank is Sergeant (500 - 2000 Reputation Level)KiReSt User rank is Sergeant (500 - 2000 Reputation Level)KiReSt User rank is Sergeant (500 - 2000 Reputation Level)KiReSt User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 9 h 3 m 45 sec
Reputation Power: 12
Dynamical file include

Hello,
the code below will allow you to include files dynamically.


Code:
<%
'Funtions to load Application
	'FileExist Function
		Function FileExist(strPath)
			Set fs=Server.CreateObject("Scripting.FileSystemObject")
			FileExist = fs.FileExists(Server.MapPath(strPath))
			set fs=nothing
		end function
		
		'Include File Function
		Function getMappedFileAsString(byVal strFilename)
			Const ForReading = 1
			
			Set fs = Server.CreateObject("Scripting.FilesystemObject")
			Set ts = fs.OpenTextFile(Server.MapPath(strFilename), ForReading)
			getMappedFileAsString = ts.ReadAll
			ts.close
			
			Set ts = nothing
			Set fs = Nothing
		End Function
	
		'Fix Include File to include HTML source as well
		function fixInclude(content)
			out=""
			
			'position for  aspStartTag
			pos1=instr(content,"<%")
			
			'position for aspEndTag
			pos2=instr(content,"%"& ">")
			
			'if there exists aspStartTag
			if pos1>0 then
				'text content  before aspStartTag
				before= mid(content,1,pos1-1)
				
				'remove linebreaks
				before=replace(before,vbcrlf,"")
				
				'put content into a response.write
				before=vbcrlf & "response.write "" " & before & """" &vbcrlf
				
				'get code content between aspStartTag  and  aspEndTag
				middle= mid(content,pos1+2,(pos2-pos1-2))
				
				'get text content after aspEndTag
				after=mid(content,pos2+2,len(content))
				
				'recurse through the rest
				out=before & middle & fixInclude(after)
				
				'did not find any aspStartTags
			else
				'remove linebreaks in file
				content=replace(content,vbcrlf,"")
				out=vbcrlf & "response.write""" & content &""""
			end if
			
			fixInclude=out
		end function
		
		Function IncludeFile(strPath)
			if FileExist(strPath) then
				Execute fixInclude(getMappedFileAsString(strPath))
			end if
		end function
%>



To include a file you simply do this:
Code:
IncludeFile(strPath)



This example redirects the user if the file can't be found:
Code:
strFile = "library/lang.asp"
if FileExist(strFile) then
		Execute fixInclude(getMappedFileAsString(strFile))
	else
		Response.Redirect("Error.asp?error=Unable to locate lang.asp, unable to generate text.")
	end if


To include all files in a folder, for instance a function folder you can do this:
Code:
<%

'Gather avaliable function
	set fs=Server.CreateObject("Scripting.FileSystemObject")
	set fo=fs.GetFolder(Server.MapPath("library/functions/"))
	
'execute files
	for each x in fo.files
		'.asp only
		If right(x.Name, 4) = ".asp" then
			Execute fixInclude(getMappedFileAsString("library/functions/"&x.name))
		end if
	next
	set fo=nothing
	set fs=nothing
%>



The possibilities are many.

Just keep in mind that you CAN NOT use the " symbol in the HTML part, only in the ASP parts of the files. For the HTML you must replace this with ' also code like:
Code:
<input type='Password' value='<% response.Write(GenText(GlobalLanguage, 18)) %>' name='Password' />

will look like this:
Code:
<input type='Password' value='  Password ' name='Password' />

*Spaces are added before and after <% %>


Not all javascript may work becuase you can't use both " and ' but you can have the javascript functions loaded in a file on its own, like:
Code:
<script src='<%= Site&"/script.js" %>' language='javascript' type='text/javascript'></script>



Also keep in mind if you have a dynamical Traget in the <a> tag, don't use the ' around the target in the HTML. IE:
Code:
<a target='<% response.write("_top") %>' >[code] then it will look for the frame "  _top " which doest exist and it will be opened in a new window. Just remove the ' and it should help.

[code]<a target=<% response.write("_top") %> >[code]





And a last thing to keep in mind, though this shouldn't casue much trouble, just some replacing. Replace This:
[code]<%= MyString %>

with:
Code:
<% response.write(MyString) %>









And just to mention it, the IncludeFile function is not a code I have made myself. Found it on google. think it was on the site 4guysfromrolla.com or something like that. Although, i made the FileExist function, not bad, eh? xD (J/K)

Feel free to use this code. (And if you got a solution to fix the spaces or the " inside the HTML, feel free to post it here )

KiReSt

Reply With Quote
  #2  
Old May 15th, 2007, 09:04 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,266 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1Folding Points: 356912 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 12 h 6 m 3 sec
Reputation Power: 1791
nice, thanks for sharing!

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Dynamical file include


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT