1

I have a class like this, that generates hyperlinks:

class getLink(obj):
    def __init__(self, text, url):
        self.text = self.Argument(text)
        self.url = self.Argument(url)
    def render(self, context):
        return "%s %s" % (html.link(self.text(context), self.url(context)))


def link(text, url):
    return mark_safe('<a href="%s">%s</a>' % (url, title))

Then to get a Link I do thus:

getLink(text=_('Test'),fn.getUrl())

HTML result:

<a href="/python/tests/">Test</a>

I want to wrap the link inside a <div> and another inside a <li> tag. I tried to add the tags directly in getLink() but doesn't work getLink("<div>" + text=_('Test'),fn.getUrl() + "</div>"). I'm newbie in Python so sorry if I'm wrong.

Thanks for you help!

1
  • 1
    getLink("<div>" + text=_('Test'),fn.getUrl() + "</div>") is invalid syntax in Python. Commented Apr 21, 2014 at 17:22

3 Answers 3

3

I hardly understand where your code is from and if you are able to modify it.

But if you are, why not change the link function and wrap the link directly in there?

Like this :

def link(text, url):
    return mark_safe('<div><a href="%s">%s</a></div>' % (url, title))

Maybe you should be more explicit.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answer! but I want to wrap the link inside a <div> and another inside a <li> tag without creating another method or class. With your solution I can not change the tag <div> to another tag.
1

Just wrap the result of your function call in the tags you want?

wrapped_in_div = "<div>" + getLink(text=_('Test'),fn.getUrl()) + "</div>"
wrapped_in_li = "<li>" + getLink(text=_('Test'),fn.getUrl()) + "</li>"

Or you can make use of format:

wrapped_in_div = "<{0}>{1}</{0}>".format("div", getLink(text=_('Test'),fn.getUrl()))
wrapped_in_li = "<{0}>{1}</{0}>".format("li", getLink(text=_('Test'),fn.getUrl()))

Comments

0
@app.route('/')
def googlelink():
    return ('<div><a href="%s">%s</a></div>' % ("https://www.google.com","Google"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.