# Inserting a string function into a HTML template

**URL:** <https://forum.djangoproject.com/t/inserting-a-string-function-into-a-html-template/25016>\
**Category:** Templates & Frontend\
**Created:** [November 3, 2023, 3:59pm UTC](https://forum.djangoproject.com/t/inserting-a-string-function-into-a-html-template/25016 "2023-11-03T15:59:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BlauweStadTechnologi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/blauwestadtechnologi/32/15959_2.png) [@BlauweStadTechnologi](https://forum.djangoproject.com/u/BlauweStadTechnologi)\
**Post date:** [November 3, 2023, 3:59pm UTC](https://forum.djangoproject.com/t/inserting-a-string-function-into-a-html-template/25016/1 "2023-11-03T15:59:09Z")

</div>

So I want to be able to insert a function for a string variable which is to be repeated throughout my website.

I have the following function which is located in my ` __init__.py` document

```auto
def string(a):
    a = "Hello, this is the repeating string"
    return a

```

In my HTML document, I have the following to call the function:

```auto
<HTML code> {{a}} <HTML code>

```

Why doesn’t it work? It’s just silly This was how I was taught to call a function, it works in the views module when this is specific to one web page and called via the `context` variable appended to the ‘render’ function, but when I want this string function to be available to all web pages it just doesn’t want to know. Surely logic would state that this would be the case in the ` __init__.py` document.

It’s just ridiculous.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 3, 2023, 4:04pm UTC](https://forum.djangoproject.com/t/inserting-a-string-function-into-a-html-template/25016/2 "2023-11-03T16:04:32Z")

</div>

In your html template, this:

> [@BlauweStadTechnologi](#):
>
> `<HTML code> {{a}} <HTML code>`

does not call a function. It is used as the key to the context that is being passed to the render function.

Review the docs at [Templates | Django documentation | Django](https://docs.djangoproject.com/en/4.2/topics/templates/)

> [@BlauweStadTechnologi](#):
>
> when I want this string function to be available to all web pages it just doesn’t want to know

See the docs for [context processors](https://docs.djangoproject.com/en/4.2/ref/templates/api/#using-requestcontext)

I also suggest that you spend some more time perusing through the rest of the documentation. Django has a very specific and opinionated way of addressing the fundamentals of a web application. It will benefit you more to start to think about this the way that Django approaches it rather than trying to make it fit your way of thinking.

---

<div class="post-metadata">

**Author:** ![BlauweStadTechnologi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/blauwestadtechnologi/32/15959_2.png) [@BlauweStadTechnologi](https://forum.djangoproject.com/u/BlauweStadTechnologi)\
**Post date:** [November 3, 2023, 4:11pm UTC](https://forum.djangoproject.com/t/inserting-a-string-function-into-a-html-template/25016/3 "2023-11-03T16:11:45Z")

</div>

I do agree with your suggestion, and this is what I’ve been doing. The context sub processor bit I have nailed, just the calling of string variables I don’t.

Thank you x

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 3, 2023, 4:17pm UTC](https://forum.djangoproject.com/t/inserting-a-string-function-into-a-html-template/25016/5 "2023-11-03T16:17:16Z")

</div>

> [@BlauweStadTechnologi](#):
>
> So I want to be able to insert a function for a string variable which is to be repeated throughout my website.

To rephrase and to try and clarify:

You want to define a function, where the output of that function can be used in a template.

If so, there are two different possibilities - one, the function requires a parameter and, two, the function does _not_ accept parameters.

Since your example shows a parameter (the first case), then what you need to do is pass the output of that function to the context.  
e.g.,

```auto
context = { 
    ...,
    'a': string(a),
    ...
}

```

you can then reference that variable as `{{ a }}` throughout the template.
