CASTLE ROCK, Colo. — Behind a church surrounded by rolling prairie on the outskirts of this Colorado town sits a donated RV that Joe Ridenour called home for a year after he lost his job during the pandemic.

Being able to live in the RV allowed him to avoid returning to his native Kansas City, where he was afraid of backsliding into using methamphetamine.

“Without this trailer and this church, I wouldn’t be alive. The drug use would have consumed me,” said Ridenour, who now has a maintenance job at the county fairgrounds and rents a room from a friend he met at the Rock church.

Last year, the town of Castle Rock ordered the nondenominational evangelical church to stop providing shelter in the RV and a camping trailer for violating zoning regulations. The church responded by suing the town, which is between Denver and Colorado Springs.

Echoing arguments made by other churches from Oregon to Ohio, the Colorado church argues that helping those in need is religious activity protected by the Constitution.

Its lawsuit is studded with references to the Bible’s exhortations to the faithful to take care of those in need. It also notes that surrounding Douglas County, one of the richest in the United States, has no other shelters for the homeless.

The church’s property is not zoned for residential use, and regulations forbid anyone from living in an RV anywhere in Castle Rock.

On Friday, a federal judge ruled that the church can continue to shelter the homeless in the campers while the lawsuit plays out.

The town, which has said it will “rigorously defend the zoning authority of communities,” declined to comment on the ruling. In court, the town has argued that the church could find other ways to help the homeless, like opening members’ homes to them or buying a property to house them in an area zoned for residential use.

The church had to turn down a request this year to let a mother and three children younger than 7 who had been living in a car to stay in one of the trailers, Pastor Mike Polhemus said.

“The word of God actually commands us to love those that are struggling and poor and to shelter them,” he said. “That is our mandate. And we believe that actually goes above the county or city codes or whatever codes there are, that these are things that are mandated by God.”

Nearly a decade ago, the church began sheltering homeless women and children in its gym one night a week as part of a church network that took turns opening their doors to them. In 2018 the church began allowing homeless men to stay in the trailers after interviews and background checks. The church network stopped its outreach to homeless families last year, and the Rock was only able to shelter people in the trailers.

“I’m sure they will welcome people back into the RVs as soon as possible,” Jeremy Dys, senior counsel for First Liberty Institute, which is representing the church, said after Friday’s ruling.

The lawsuit is based on the church’s religious freedom under the First Amendment and a federal law intended to protect places of worship from being discriminated against in zoning decisions.

The Religious Land Use and Institutionalized Persons Act, passed with bipartisan support by Congress in 2000, bars governments from imposing land-use regulations that put a substantial burden on religious exercise without a compelling reason for doing so. It has helped a wide range of faiths build or expand places of worship but has also been invoked in legal fights over efforts to help the homeless.

Before the Rock filed its lawsuit, a church in Bryan, Ohio, filed a similar federal case this year when its pastor was criminally charged for allowing homeless people to shelter there. The church and city officials are trying to negotiate a resolution to the lawsuit, which also says the city violated the 2000 federal law.

Two other recent lawsuits also alleged a violation of that law.

In March, a federal judge ruled that the city of Brookings, Oregon, could not limit a church’s homeless meal services. St. Timothy’s Episcopal Church’s lawsuit said an ordinance limiting the program to two days a week and requiring a permit also violated its right to freely practice religion.

Last year, a Christian nonprofit penalized and threatened with prosecution in Santa Ana, California, for feeding homeless people settled its lawsuit against the city after the Justice Department weighed in.

Lauren Langer, an attorney in Los Angeles who represents cities in land-use cases, said lawsuits pitting houses of worship against municipalities over care for homeless people come up periodically but they can be costly and drag on.

Some California communities and churches have taken a different approach — forming partnerships to set up places where people living in vehicles can park overnight while providing services like restrooms, trash pickup and security, she said.

Sonia Moran, who lives with her husband and two sons in a home bordering the Castle Rock church’s property, wasn’t initially concerned when she learned it was providing shelter to the homeless in the trailer. But that changed after it proposed building an affordable housing development on its 54-acre property.