Question:
I want palceholders in strings.
Something like this: {{*}}
so basically a '{{*}}' == '{{name}}
statement is True
.
I also want to collect the value of *
so something like this:
string.replace('Hi *', 'Hello *!')
Repl Link:
link: https://replit.com/@SnakeyKing/chocolatepy#chocolate.py
(don’t ask why I thought of a lame name for my template engine)
Code:
contents = contents.replace('{{*}}', vars['*'])
in python? use an f-string
a = 1+2
print(f" My var: {a} is 3")
1 Like
no I meant the value of *
can be anything
So, regex? I’m a bit confused.
in flask? or in normal python strings
so these statements are True
:
('Hello *!' == 'Hello World!')
('* + *' == '243 + abc')
oh, well then you shouldn’t have them “equal”. they should be subsets
QwertyQwerty88:
So, regex?
yes. (but the length of the *
can be anything)
normal python
I also want to collect the value of *
so smth like this:
string.replace('Hi *', 'Hello *!')
import re
re.match('Hello .*!', 'Hello World!')
re.match('.* + .*', '243 + abc')
Matches everythin except newline characters
I tried re.match
but it didn’t suit what I was doing.
This what I’m actually doing:
contents = contents.replace('{{*}}', vars['*'])
so basically something like Jinja2
import re
match = re.match('Hello (.*)!', 'Hello World!')
if match:
print(match.group(1)) # Outputs: World
match = re.match('(.*) + (.*)', '243 + abc')
if match:
print(match.group(1)) # Outputs: 243
print(match.group(2)) # Outputs: abc
Didn’t you read:
re.match
doesn’t help, and I’m not matching anything. I’m just replacing.
SnakeyKing:
I’m just replacing
Ohh I’m dumb.
import re
# Replace 'World' with 'Python'
result = re.sub('Hello (.*)!', 'Hello Python!', 'Hello World!')
print(result) # Outputs: Hello Python!
# Replace '243' with '123' and 'abc' with 'xyz'
result = re.sub('(.*) + (.*)', '123 + xyz', '243 + abc')
print(result) # Outputs: 123 + xyz
QwertyQwerty88:
import re
# Replace 'World' with 'Python'
result = re.sub('Hello (.*)!', 'Hello Python!', 'Hello World!')
print(result) # Outputs: Hello Python!
# Replace '243' with '123' and 'abc' with 'xyz'
result = re.sub('(.*) + (.*)', '123 + xyz', '243 + abc')
print(result) # Outputs: 123 + xyz
how can I use that in this:
contents = contents.replace('{{*}}', variables['*'])
Also I want to replace anything inside {{
and }}
with the value of the key that has the name of the value inside {{
and }}
that is inside the variables
dict.
You know what, scratch that, why not just use Jinja (lol)
from jinja2 import Template
contents = Template(contents).render(vars)
QwertyQwerty88:
why not just use Jinja
I’m making a template engine
If I use Jinja then I basically remake Jinja
LOLOL ok ok
You could always use .format
, but ofc that only uses {}
and not {{}}
# Replace the placeholders with the values
contents = contents.format(**vars)
print(contents)
so you could instead use
import re
def replace(match):
key = match.group(1)
return vars.get(key, match.group(0))
contents = re.sub('{{(.*)}}', replace, contents)
print(contents)
(note I haven’t actually tested that)
wait what is var? I thought it was a dictionary
wait never mind
but still… the variables need to be handed to the render
function, not the replace
function
so is there a way to do it with only one function?