Skip to content

Commit ea8345c

Browse files
committed
Merge pull request #1 from PhoenixMachina/v0.5
V0.5
2 parents c562be5 + 99b0bad commit ea8345c

File tree

6 files changed

+51
-23
lines changed

6 files changed

+51
-23
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ In your code, you need to have wherever you want to use it :
1919
using TlalocTemplate
2020
```
2121

22-
You need to create a tlaloc object and set the path to your config file :
22+
You need to create a Tlaloc object and set the path to your config file :
2323
```
2424
tlaloc = TlalocEngine("path/to/conf.ini")
2525
```
2626

2727
Inside your conf.ini, you need to have :
2828
```
2929
viewPath=pathWithYourViews
30-
TemplatePath=pathWithYourTemplates
31-
ResourcePath=pathWithYourResources
30+
templatePath=pathWithYourTemplates
31+
resourcePath=pathWithYourResources
3232
```
3333
By resources, we mean like css, javascript, all that stuff. Doesn't matter if they're in a subfolder.
3434

@@ -44,5 +44,12 @@ addArg(mypage,name,value)
4444

4545
Here's a look at what your view file could look like :
4646
```
47+
${extends "header.html"}
4748
Hey to you my friend ${username}! What's up?
4849
```
50+
51+
##Render
52+
Obviously you will want at some point to render what you've done. It's very easy :
53+
```
54+
render(mypage)
55+
```

src/Tlaloc.jl

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,29 @@ keywords = ["extends","for","endfor","addResource"] #Not all implemented yet
99
#Type Tlaloc
1010
type TlalocEngine
1111
viewPath::ASCIIString # path to views
12-
TemplatePath::ASCIIString # path to templates
13-
ResourcePath::ASCIIString # path to resources
12+
templatePath::ASCIIString # path to templates
13+
resourcePath::ASCIIString # path to resources
1414
#Constructor
1515
function TlalocEngine(path::ASCIIString="")
1616
if path != ""
1717
conf = ConfParse(path)
1818
parse_conf!(conf)
1919
viewPath = retrieve(conf, "default", "viewPath")
20-
TemplatePath = retrieve(conf, "default", "TemplatePath")
21-
ResourcePath = retrieve(conf, "default", "ResourcePath")
20+
templatePath = retrieve(conf, "default", "templatePath")
21+
resourcePath = retrieve(conf, "default", "resourcePath")
22+
23+
if((viewPath[end-1:end] != "/") && (viewPath[end-1:end] != "\\"))
24+
viewPath = string(viewPath,"/")
25+
end
26+
if((templatePath[end-1:end] != "/") && (templatePath[end-1:end] != "\\"))
27+
templatePath = string(templatePath,"/")
28+
end
29+
if((resourcePath[end-1:end] != "/") && (resourcePath[end-1:end] != "\\"))
30+
resourcePath = string(resourcePath,"/")
31+
end
32+
2233
end
23-
new(viewPath, TemplatePath, ResourcePath)
34+
new(viewPath, templatePath, resourcePath)
2435
end
2536
end
2637

@@ -44,22 +55,28 @@ end
4455
function parseView(page::Page)
4556
response = open(readall, page.tlaloc.viewPath * page.view)
4657
difference = 0 # We need this because eachMatch collects all the match and then treats them, which means the data concerning indexes starting from the second match needs to be adjusted
47-
for match in eachmatch(r"\$\{([a-zA-Z0-9_ ]+)\}",response)
48-
58+
for amatch in eachmatch(r"\$\{([a-zA-Z0-9_ .\"]+)\}",response)
4959
for keyword in keywords
5060
reg_string = "$(keyword)"
5161
reg = Regex(reg_string)
52-
if ismatch(reg,match.match)
62+
if ismatch(reg,amatch.match)
5363
if keyword == "extends"
54-
# Soon
64+
if ismatch(Regex("extends \"([a-zA-Z0-9_. ]+)\""),amatch.match)
65+
statement = match(Regex("\"([a-zA-Z0-9_. ]+)\""),amatch.match)
66+
content = open(readall,page.tlaloc.templatePath * (statement.match)[2:end-1])
67+
response = string(response[1:(amatch.offset)-1 + difference],content,response[((amatch.offset)+difference+(length(amatch.match))):end] )
68+
difference = difference + length(content) - length(amatch.match)
69+
end
70+
elseif keyword == "addResource"
71+
#Soon
5572
end
5673
end
5774
end
5875

59-
if haskey(page.args,(match.match)[3:end-1])
60-
var = (page.args)[(match.match)[3:end-1]]
61-
response = string(response[1:(match.offset)-1 + difference],var,response[((match.offset)+difference+(length(match.match))):end] )
62-
difference = difference + length(var) - length(match.match)
76+
if haskey(page.args,(amatch.match)[3:end-1])
77+
var = (page.args)[(amatch.match)[3:end-1]]
78+
response = string(response[1:(amatch.offset)-1 + difference],var,response[((amatch.offset)+difference+(length(amatch.match))):end] )
79+
difference = difference + length(var) - length(amatch.match)
6380
end
6481
end
6582

test/apage.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
${extends} ${something} Hello ${name}, great to meet you!
1+
${extends "testHeader.html"} ${something} Hello ${name}, great to meet you!

test/runtests.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ using Tlaloc
44
# Testing constructor
55
engine = TlalocEngine(string(dirname(Base.source_path()),"/test_conf.ini"))
66
@test typeof(engine) == TlalocEngine
7-
@test engine.viewPath == "thisIsTheViewPath"
8-
@test engine.TemplatePath == "thisIsTheTemplatePath"
9-
@test engine.ResourcePath == "thisIsTheResource"
7+
@test engine.viewPath == "thisIsTheViewPath/"
8+
@test engine.templatePath == "thisIsTheTemplatePath/"
9+
@test engine.resourcePath == "thisIsTheResource/"
1010

1111
#Testing Page constructor
1212
aPage = Page(engine,"apage.html",Dict())
@@ -20,4 +20,5 @@ addArg(aPage,"name","aValue")
2020

2121
#Testing view
2222
aPage.tlaloc.viewPath = string(dirname(Base.source_path()),"/")
23-
@test render(aPage) == "\${extends} \${something} Hello aValue, great to meet you!\n"
23+
aPage.tlaloc.templatePath = string(dirname(Base.source_path()),"/")
24+
@test render(aPage) == "Yop\n\nBest header ever\n \${something} Hello aValue, great to meet you!\n"

test/testHeader.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Yop
2+
3+
Best header ever

test/test_conf.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
viewPath=thisIsTheViewPath
2-
TemplatePath=thisIsTheTemplatePath
3-
ResourcePath=thisIsTheResource
2+
templatePath=thisIsTheTemplatePath
3+
resourcePath=thisIsTheResource

0 commit comments

Comments
 (0)