Ver Mensaje Individual
  #109 (permalink)  
Antiguo 21/09/2008, 21:20
Alert
 
Fecha de Ingreso: junio-2004
Mensajes: 72
Antigüedad: 19 años, 10 meses
Puntos: 2
Respuesta: Biblioteca de Clases,Funciones y Sub-rutinas.

Include Dinamico en ASP
Simple , facil y rapido....

La idea de este post es mostrar como generar un include dinamico de la siguiente forma...
Código asp:
Ver original
  1. <%
  2. Inc_File "mi_archivo_a_incluir.asp"
  3. 'Puede ser cualquier tipo de archivo
  4. 'Asp, Inc, Tpl, etc....
  5. %>

Ahora lo bueno de esta clase es que permite la ejecusion de codigo incluido en los archivos..
si por el caso yo tengo en un archivo X , una funsion donde apunto a la DB para mostrar/modificar datos en la misma.. al generar el Inlucde , trabaja de la misma forma que si estubieras en esa pagina en concreto.

Ademas funsiona tanto para los 2 casos de Include FILE y VIRTUAL

La clase Provee un sistema de Error en caso de no encontrar el archivo a incluir. (Aclaro que esta en fase beta esta parte)
Genera el error de inclusion y continua la ejecusion de la pagina.
estoy implementando un sistema mejorado para esta seccion

Codigo de la calse.
Class.asp
Código asp:
Ver original
  1. <%
  2. Public Inc_File, Include_Vars, Include_Vars_Count
  3. Dim ObjFsChk
  4.  
  5. Set Inc_File = New Csl_Include
  6. Set ObjFsChk = Server.CreateObject("Scripting.FileSystemObject")
  7. Set RegFltr = New regexp
  8. Set RegFltr2 = New regexp
  9.  
  10. Class Csl_Include
  11.    
  12.     Private Sub Class_Initialize()
  13.         Set Include_Vars = Server.CreateObject("Scripting.Dictionary")
  14.         Include_Vars_Count = 0
  15.     End Sub
  16.    
  17.     Private Sub Class_Deactivate()
  18.         Include_Vars.RemoveAll
  19.         Set Include_Vars = Nothing
  20.         Set Include = Nothing
  21.     End Sub
  22.    
  23.     Public Default Function Inc_File(ByVal Str_Path)
  24.         Dim Str_Source
  25.         IF Str_Path <> "" Then
  26.             Init_Path = Str_Path
  27.             Str_Source = ReadFile(Str_Path)
  28.             IF Str_Source <> "" Then
  29.                 Str_Source = ProcessIncludes (Str_Source, Init_Path, 0)
  30.                 ConvertToCode Str_Source
  31.                 FormatCode Str_Source
  32.                 IF Str_Source <> "" Then
  33.                     ExecuteGlobal Str_Source
  34.                 End IF
  35.             End IF
  36.             'Esto es simplemente una variante al mostrar el error de include
  37.             IF Not ObjFsChk.FileExists(Str_Path) Then
  38.                 Response.Write "<table border='1' cellspacing='0' cellpadding='0' align='center'><tr><td>" & VbCrLf
  39.                 Response.Write "<font color=""Red"">Error:<BR></font>" & VbCrLF
  40.                 Response.Write "El archivo <font color=""Red"">" & Str_Path & "</font> No se pudo Incluir"
  41.                 Response.Write "</td></tr></table>"
  42.             End IF
  43.         End IF
  44.     End Function
  45.    
  46.     Private Sub ConvertToCode(Str_Source)
  47.         Dim i, Str_Temp, Arr_Temp, Int_Len, BaseCount
  48.         BaseCount = Include_Vars_Count
  49.         IF Str_Source <> "" Then
  50.             Str_Temp = Replace(Str_Source,"<" & "%","¿%")
  51.             Str_Temp = Replace(Str_Temp,"%" & ">","¿")
  52.             IF Left(Str_Temp,1) = "¿" Then Str_Temp = Right(Str_Temp,Len(Str_Temp) - 1)
  53.             IF Right(Str_Temp,1) = "¿" then Str_Temp = Left(Str_Temp,Len(Str_Temp) - 1)
  54.             Arr_Temp = Split(Str_Temp,"¿")
  55.             Int_Len = Ubound(Arr_Temp)
  56.             IF (Int_Len + 1) > 0 Then
  57.                 For i = 0 To Int_Len
  58.                     Str_Temp = Arr_Temp(i)
  59.                     Str_Temp = Replace(Str_Temp,VbCrLf & VbCrLf,VbCrLf)
  60.                     IF Left(Str_Temp,2) = VbCrLf Then Str_Temp = Right(Str_Temp,Len(Str_Temp) - 2)
  61.                     IF Right(Str_Temp,2) = VbCrLf Then Str_Temp = Left(Str_Temp,Len(Str_Temp) - 2)
  62.                     IF Left(Str_Temp,1) = "%" Then
  63.                         Str_Temp = Right(Str_Temp,Len(Str_Temp) - 1)
  64.                         IF Left(Str_Temp,1) = "=" Then
  65.                             Str_Temp = Right(Str_Temp,Len(Str_Temp) - 1)
  66.                             Str_Temp = "Response.Write " & Str_Temp
  67.                         End IF
  68.                     Else
  69.                         IF Str_Temp <> "" Then
  70.                             Include_Vars_Count = Include_Vars_Count + 1
  71.                             Include_Vars.Add Include_Vars_Count, Str_Temp
  72.                             Str_Temp = "Response.Write Include_Vars.Item(" & Include_Vars_Count & ")"
  73.                         End IF
  74.                     End IF
  75.                     IF Right(Str_Temp,2) <> VbCrLf Then Str_Temp = Str_Temp
  76.                     Arr_Temp(i) = Str_Temp
  77.                 Next
  78.                     Str_Source = Join(Arr_Temp,VbCrLf)
  79.             End IF
  80.         End IF
  81.     End Sub
  82.    
  83.     Private Function ProcessIncludes(Tmp_Source, CurDir, CurDepth)
  84.         Dim Int_Start, Str_Path, Str_Mid, Str_Temp, LocalDir
  85.         IF (CurDepth < 20) Then
  86.             LocalDir = Left(CurDir, Len(CurDir)-Len(ObjFsChk.GetFileName(CurDir)))
  87.             Tmp_Source = Replace(Tmp_Source,"<!-- #","<!--#")
  88.             Int_Start = InStr(Tmp_Source,"<!--#Include")
  89.             Str_Mid = Lcase(GetBetween(Tmp_Source,"<!--#Include","-->"))
  90.             Do Until Int_Start = 0
  91.                 Str_Mid = Lcase(GetBetween(Tmp_Source,"<!--#Include","-->"))
  92.                 IF (Str_Mid <> "") Then Int_Start = 1
  93.                 IF Int_Start >  0 Then
  94.                     Str_Method = Lcase(Trim(GetBetween(Str_Mid," ","=")))
  95.                     Str_Temp = Lcase(GetBetween(Str_Mid,chr(34),Chr(34)))
  96.                     Str_Temp = Trim(Str_Temp)
  97.                     IF (Str_Method = "File") Then
  98.                         Newdir = ObjFsChk.BuildPath(LocalDir,Replace(Str_Temp,"/","\"))
  99.                         Str_Path = ProcessIncludes(ReadFile(Newdir), Newdir, CurDepth+1)
  100.                         Tmp_Source = Replace(Tmp_Source,"<!--#Include" & Str_Mid & "-->",Str_Path & VbCrLf)
  101.                     ElseIF (Str_Method = "Virtual") Then
  102.                         Newdir = Server.MapPath(Str_Temp)
  103.                         Str_Path = ProcessIncludes(ReadFile(Newdir), Newdir)
  104.                         Tmp_Source = Replace(Tmp_Source,"<!--#Include" & Str_Mid & "-->",Str_Path & VbCrLf)
  105.                     Else
  106.                         Tmp_Source = Replace(Tmp_Source,"<!--#Include" & Str_Mid & "-->","" & VbCrLf)
  107.                     End IF
  108.                 End IF
  109.                 Int_Start = InStr(Tmp_Source,"<!--#Include")
  110.             Loop
  111.             ProcessIncludes = Tmp_Source
  112.         Else
  113.             ProcessIncludes = ""
  114.         End IF
  115.     End Function
  116.    
  117.     Private Sub FormatCode(Str_Code)
  118.         Dim i, Arr_Temp, Int_Len
  119.         Str_Code = Replace(Str_Code,VbCrLf & VbCrLf,VbCrLf)
  120.         IF Left(Str_Code,2) = VbCrLf Then Str_Code = Right(Str_Code,Len(Str_Code) - 2)
  121.         Str_Code = Trim(Str_Code)
  122.         IF InStr(Str_Code,VbCrLf) > 0 Then
  123.             Arr_Temp = Split(Str_Code,VbCrLf)
  124.             For i = 0 To Ubound(Arr_Temp)
  125.                 Arr_Temp(i) = ltrim(Arr_Temp(i))
  126.                 IF Arr_Temp(i) <> "" Then Arr_Temp(i) = Arr_Temp(i) & VbCrLf
  127.             Next
  128.             Str_Code = Join(Arr_Temp,"")
  129.             Arr_Temp = VbNull
  130.         End IF
  131.     End Sub
  132.    
  133.     Private Function ReadFile(Str_Path)
  134.         Dim ObjFile
  135.         IF Str_Path <> "" Then
  136.             IF InStr(Str_Path,":") = 0 Then Str_Path = Server.MapPath(Str_Path)
  137.             IF ObjFsChk.FileExists(Str_Path) Then
  138.                 Set ObjFile = ObjFsChk.OpenTextFile(Str_Path, 1, False)
  139.                 IF Err.Number = 0 Then
  140.                     IF (Not ObjFile.AtEndOfStream) Then
  141.                         ReadFile = ObjFile.ReadAll
  142.                         RegFltr.Global = True
  143.                         RegFltr.IgnoreCase = True
  144.                         RegFltr.Pattern = "<%[^=](.|\n)*?%" & ">"
  145.                         RegFltr2.Global = True
  146.                         RegFltr2.IgnoreCase = True
  147.                         RegFltr2.Pattern = """.*?"""
  148.                         Set Matches = RegFltr.Execute(ReadFile)
  149.                         pEnd = 0
  150.                         pStart = 1
  151.                         Str_Temp_New = ""
  152.                         For Each Match In Matches
  153.                             pEnd = Match.FirstIndex + 1
  154.                             IF (pEnd <> pStart) Then
  155.                                 Str_Temp_New = Str_Temp_New & Mid(ReadFile, pStart, pEnd - pStart)
  156.                                 pStart = pEnd
  157.                             End IF
  158.                             Set Matches2 = RegFltr2.Execute(Match.Value)
  159.                             pEnd2 = 0
  160.                             pStart2 = 1
  161.                             cpystr = ""
  162.                             For Each Match2 in Matches2
  163.                                 pEnd2 = Match2.FirstIndex + 1
  164.                                 IF (pEnd2 <> pStart2) Then
  165.                                     cpystr = cpystr & Mid(Match.Value, pStart2, pEnd2 - pStart2)
  166.                                     pStart2 = pEnd2
  167.                                 End IF
  168.                                 cpystr = cpystr & Replace(Match2.value,"'","æ")
  169.                                 pEnd2 = pStart2 + Match2.length
  170.                                 pStart2 = pEnd2
  171.                             Next
  172.                             IF (pEnd2 < Len(Match.Value)) Then
  173.                                 pEnd2 = Len(Match.Value) + 1
  174.                                 cpystr = cpystr & Mid(Match.Value, pStart2, pEnd2 - pStart2)
  175.                             End IF
  176.                             Set Matches2 = Nothing
  177.                             RegFltr.Pattern = "'.*?\n"
  178.                             Str_Temp_New = Str_Temp_New & RegFltr.Replace(cpystr,vbcr)
  179.                             pEnd = pStart + Match.length
  180.                             pStart = pEnd
  181.                         Next
  182.                         IF (pEnd < len(ReadFile)) Then
  183.                             pEnd = len(ReadFile) + 1
  184.                             Str_Temp_New = Str_Temp_New & Mid(ReadFile, pStart, pEnd - pStart)
  185.                         End IF
  186.                         ReadFile = Replace(Str_Temp_New,"æ","'")
  187.                     Else
  188.                         ReadFile = ""
  189.                     End IF
  190.                     ObjFile.close
  191.                 End IF
  192.                 Set ObjFile = Nothing
  193.             End IF
  194.         End IF
  195.     End Function
  196.    
  197.     Private Function GetBetween(StrData, StrStart, StrEnd)
  198.         Dim IngStart, IngEnd
  199.         IngStart = InStr(StrData, StrStart) + Len(StrStart)
  200.         IF (IngStart <> 0) Then
  201.             IngEnd = InStr(IngStart, StrData, StrEnd)
  202.             IF (IngEnd <> 0) Then
  203.                 GetBetween = Mid(StrData, IngStart, IngEnd - IngStart)
  204.             End IF
  205.         End IF
  206.     End Function
  207. End Class
  208. %>

Y por ultimo quedaria generar una pagin Supongamos Index.asp donde hacemos la llamada a nuestra clase.
Código asp:
Ver original
  1. <!--#INCLUDE FILE = "Class.asp" -->
  2. <%
  3. Inc_File "Include/Funsiones.asp"
  4. Inc_File "Configuracion.asp"
  5. 'y asi con todos los archivos que necesitemos incluir
  6. %>

Espero que les sea de utilidad para todos y aquel que quiera modificar el codigo tiene todo el derecho....para que es el Codigo Libre sino???

Este es mi pequeño aporte (Tarde pero seguro) jajaja