<commit_before># -*- coding: utf-8 -*-

from django import template
from django.template.base import Node, TemplateSyntaxError
from django.utils.encoding import smart_text

register = template.Library()


class FirstOfAsNode(Node):
    def __init__(self, vars, variable_name=None):
        self.vars = vars
        self.variable_name = variable_name

    def render(self, context):
        for var in self.vars:
            value = var.resolve(context, True)

            if value:
                if self.variable_name:
                    context[self.variable_name] = value
                    break

                else:
                    return smart_text(value)

            return ''


@register.tag(name="firstofas")
def do_firstofas(parser, token):
    """ Original idea: https://code.djangoproject.com/ticket/12199 """

    bits = token.split_contents()[1:]
    variable_name = None
    expecting_save_as = bits[-2] == 'as'

    if expecting_save_as:
        variable_name = bits.pop(-1)
        bits = bits[:-1]<commit_msg>Fix the `firstofas` template tag returning '' too early.<commit_after># -*- coding: utf-8 -*-

from django import template
from django.template.base import Node, TemplateSyntaxError
from django.utils.encoding import smart_text

register = template.Library()


class FirstOfAsNode(Node):
    def __init__(self, args, variable_name=None):
        self.vars = args
        self.variable_name = variable_name

    def render(self, context):
        for var in self.vars:
            value = var.resolve(context, True)

            if value:
                print('FOUND %s: %s' % (self.variable_name, value))

                if self.variable_name:
                    context[self.variable_name] = value
                    break

                else:
                    return smart_text(value)

        return ''


@register.tag
def firstofas(parser, token):
    """ Original idea: https://code.djangoproject.com/ticket/12199 """

    bits = token.split_contents()[1:]
    variable_name = None
    expecting_save_as = bits[-2] == 'as'

    if expecting_save_as:
        variable_name = bits.pop(-1)
        bits = bits[:-1]
{'fraction_non_alphanumeric': 0.1064322073114299, 'fraction_numerical': 0.00925497454881999, 'mean_word_length': 2.226865671641791, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5614921', 'n_tokens_mistral': 639, 'n_tokens_neox': 598, 'n_words': 149}